Suppose I want to compute $(a_1 + a_2 + ... + a_n) \mod m $. For very large values of $a_i$, I can take modulo after every operation: $ [(a_1 \mod m) + (a_2 \mod m) + ... + a_n \mod m] \mod m$ (I don't want the values to exceed their data-type's size). This doesn't work when there's a division operation in the expression.
Specifically I have this expression: $[(a-b)/5] \mod m $
which is not equal to: $ [(a \mod m) - (b \mod m)]/5 \mod m $
because after taking modulo, the numerator will not necessarily be divisible by the denominator ( a-b will always be divisible by 5).
What is the work-around to this problem?