Modular exponentiation

Write codeMedium18 min

Your task

Compute (base ** exp) % mod for non-negative integers base, exp, and a positive integer mod. The exponent exp can be as large as 10^9, so you cannot multiply exp times in a loop, and base**exp itself is far too large to materialize. Return the result as an integer in [0, mod). Note that when mod == 1 the answer is always 0.

Examples

fast_pow_mod(base: int, exp: int, mod: int) -> int
Example 1
Inputfast_pow_mod(2, 10, 1000)
Output24