Compute xⁿ mod m when n is as large as 10⁹. Multiplying n times is far too slow. Fast exponentiation squares instead:
xⁿ = (x^(n/2))² if n is even
xⁿ = x × (x^(n/2))² if n is odd
Iteratively, walk through the bits of n, squaring x each step and multiplying it into the result when the bit is 1. That takes O(log n) multiplications.
Overflow: with m near 10⁹, a product reaches 10¹⁸, past JavaScript's exact range of 2⁵³. Use BigInt in JS and 64-bit ints in C++ or Java. Python handles it automatically.