Solution (source code)

= Solution

In <modular arithmetic>, calculations may be performed after replacing each input by its remainder. For a positive modulus $m$, write $[a]_m$ for the remainder in $\{0,\ldots,m-1\}$. Then
$$
\boxed{\begin{aligned}
[a+b]_m&=[[a]_m+[b]_m]_m,\\
[ab]_m&=[[a]_m[b]_m]_m,\\
[a^k]_m&=[[a]_m^k]_m\qquad(k\geq0).
\end{aligned}}
$$
Equivalently, if $a\equiv a'\pmod m$ and $b\equiv b'\pmod m$, addition and multiplication give $a+b\equiv a'+b'$ and $ab\equiv a'b'$, and a fixed nonnegative integer exponent gives $a^k\equiv(a')^k$. One cannot in general reduce the exponent modulo $m$ by this rule.