Naive a**e is O(e) multiplies and overflows immediately. Binary exponentiation: a^e = (a^{e/2})² when e is even, a · a^{e-1} when odd. O(log e) multiplies.
CSES Exponentiation: a, e ≤ 1e9, mod 1e9+7. You must reduce as you go. JS Number is safe for (a * b) % mod only if a*b < 2^53. 1e9+7 squared is ~1e18, which is over 2^53. Use BigInt in the arena for the real modulus, or keep the teaching mod small. C++ uses __int128 or a careful 1LL * a * b % MOD.
function modPow(a, e, mod) {
a = ((a % mod) + mod) % mod;
let base = BigInt(a);
let exp = BigInt(e);
const m = BigInt(mod);
let ans = 1n;
while (exp > 0n) {
if (exp & 1n) ans = (ans * base) % m;
base = (base * base) % m;
exp >>= 1n;
}
return Number(ans);
}Checks 0/2