I need  an efficient algorithm with clear steps to compute $a^n \bmod p$ when $n$ is large enough and $p$ also. I'm looking for something equivalent to the built-in PowerMod function in Mathematica.
Searching for efficient method to compute $a^n \bmod p$ for large numbers
reference request;discrete mathematics