I have found that the same mod operation produces different results depending on what language is being used.
In Python:
-1 % 10
produces 9
In C it produces -1 !
((n % M) + M) % M
to get the same result as in Python. E. g. ((-1 % 10) + 10) % 10
. Note, how it still works for positive integers: ((17 % 10) + 10) % 10 == 17 % 10
, as well as for both variants of C implementations (positive or negative remainder).