The modulo operator is just like the mod operator when the number is positive, but different if the number is negative.
Many a times in the problems we are asked to give the answer modulo 10^9+7.
Let the answer(before using modulo) be denoted by ‘a’.
Simple Straightforward Rule-
if a is positive, then a modulo 10^9+7= a%(10^9+7)
if a is negative, then a modulo 10^9+7= (a%(10^9+7))+(10^9+7)
If, in such problems, we find that any step of the loop may calculate a value that is out of the integer range (if we are using integers), then we can use the modulo operator in that step itself. The final answer will be as if we have used the modulo operator only once.
This is because- (a**b)%c=((a%c)*(b%c))%c
Same goes for addition and subtraction.