Solution (source code)

= Solution

The <Fermat-Euler theorem> states that, for an <integer> $N\geq2$ and any <integer> $a$ <coprime> to $N$,
$$
\boxed{a^{\varphi(N)}\equiv1\pmod N,}
$$
where the <Euler totient function> $\varphi(N)$ counts the <residue classes> <coprime> to $N$.

Let $r_1,\ldots,r_{\varphi(N)}$ be representatives of these <residue classes>. Multiplication by $a$ gives a <permutation> of them: each $ar_i$ remains <coprime> to $N$, and $ar_i\equiv ar_j$ implies $r_i\equiv r_j$ by cancellation using the <modular inverse> of $a$. Multiplying all the resulting <modular congruences> yields
$$
a^{\varphi(N)}r_1\cdots r_{\varphi(N)}\equiv r_1\cdots r_{\varphi(N)}\pmod N.
$$
The product is itself <coprime> to $N$, so it has a <modular inverse> and can be canceled. This proves the <Fermat-Euler theorem>. For $N=1$, the congruence is trivial; no separate argument is needed.