Andreas,
The problem comes from the term1 of your bed load computation. U2D/V2D can be negative. So when you raise this number to the power 2.97, ie exp(2.97*log(U2D)), log(negative values) gives you NaN numbers, not especially on the boundaries but inside your model domain.
I didn't know the formulation from Karim & Kennedy (1983), but you can look more closely at how they compute these terms. If it's not explained, you can try to take the absolute value of U2D/V2D to compute the term1 and then assign the sign of U2D/V2D to the QBX/QBY values.
I hope it helps.
Regards,
Laurent