Let $n \geqslant 2$. Count the number of $x$ in $\llbracket 1, n \rrbracket$ such that $x^{2}=x(\bmod n)$.
The first reflex is to factorize: we are looking for the number of $x$ in $\llbracket 1, n \rrbracket$ such that $x(x-1)=0(\bmod n)$. First, let's handle the case where $n$ is a power of a prime number different from 1. Since $x$ and $x-1$ are coprime (because $x-(x-1)=1$), $n$ divides $x(x-1)$ if and only if $n$ divides $x$ or $x-1$. Thus, the equation is equivalent, as $x$ is in $\llbracket 1, n \rrbracket$, to $x=n$ or $x=1$: there are two solutions.

Now we need to handle the general case. First, we can note that the number of solutions is actually the number of congruence classes modulo $n$ such that $x(x-1)=0$ $(\bmod n)$. To reduce to the previous case, we use the Chinese Remainder Theorem. Let $n=p_{1}^{a_{1}} \ldots p_{k}^{a_{k}}$ with the $p_{i}$ being prime numbers and the $a_{i}$ being strictly positive integers. The equation is equivalent to, for all $i \in \llbracket 1, k \rrbracket$, $p_{i}^{a_{i}}$ divides $x(x-1)$, which is equivalent to, for all $i \in \llbracket 1, k \rrbracket$, $x \equiv 0\left(\bmod p_{i}^{a_{i}}\right)$ or $x=1\left(\bmod p_{i}^{a_{i}}\right)$. The equation is therefore equivalent to the existence of $\left(e_{1}, \ldots, e_{k}\right)$ in $\{0,1\}^{k}$ such that for all $i \in \llbracket 1, k \rrbracket$, $x \equiv e_{i}\left(\bmod p_{i}^{a_{i}}\right)$. The $p_{i}^{a_{i}}$ being pairwise coprime, if we fix $\left(e_{1}, \ldots, e_{k}\right)$ in $\{0,1\}^{k}$, the system for all $i \in \llbracket 1, k \rrbracket$, $x=e_{i}\left(\bmod p_{i}^{a_{i}}\right)$ has a unique solution modulo $n$. Each solution class is distinct: if we are given $\left(e_{1}, \ldots, e_{k}\right) \neq\left(e_{1}^{\prime}, \ldots, e_{k}^{\prime}\right)$ in $\{0,1\}^{k}$ and denote $a$ the solution modulo $n$ of $x \equiv e_{i}\left(\bmod p_{i}^{a_{i}}\right)$, and $b$ the solution of $x \equiv e_{i}^{\prime}\left(\bmod p_{i}^{a_{i}}\right)$, suppose by contradiction $a=b$, in this case for all $i$, $e_{i} \equiv a \equiv b \equiv e_{i}^{\prime}\left(\bmod p_{i}^{a_{i}}\right)$ and since the $e_{i}$ and $e_{i}^{\prime}$ are in $\{0,1\}$, we get $e_{i}=e_{i}^{\prime}$ which is contradictory. In particular, there are as many solution classes as there are $\left(e_{1}, \ldots, e_{k}\right)$ in $\{0,1\}^{k}$, which is $2^{k}$, with $k$ being the number of distinct prime divisors of $n$.

In fact, the Chinese Remainder Theorem tells us that the number of solutions here modulo $n$ is the same as the product for all $i$ of the number of solutions modulo $p_{i}^{a_{i}}$.