I found this snippet of code that calculates $n! \bmod m$ iteratively.
long long x = 1;
for (int i = 2; i <= n; i++) {
x = (x*i)%m;
}
cout << x%m;
Now, I'm still new to modular arithemtic. I know that $(a\cdot b) \bmod m = (a \bmod m \cdot b \bmod m ) \bmod m$.
But the code doesn't seem to be doing this. The code is doing:
$x = (1 \cdot2) \bmod m $
$x = (2 \cdot3) \bmod m $
$x = (6 \cdot4) \bmod m $
etc...
Where does the rule come into play in this code?
Shouldn't we be doing
x = x * (i % m) ?