Forgive me if this question does not belong on this site for it is simplistic and this is my first post, however I do not seem to understand the modulo function when it comes to negative numbers.
I'd assume the process for calculating modulo would be the same as with positive numbers:
- 9 % 7 = 2 because 9 - (7 * 9/7) = 9 - (7 * 1) = 2
So wouldn't 9 % (-7) = 9 - (-7 * -9/7) = 9 - (-7 * -1) = 2?


Note that $-5 = -7 +2$ and $9 = -(-7)+2$. When you do these calculations, your remainder is always nonnegative by definition (just to make things easily stated) but your coefficient $q$ (in $p = qd+r$) can be any number in $\Bbb Z$. This is why $-1$ is admissible as a coefficient in front of $-7$ when decomposing $9$.