Power of a Number, Modulo 10⁹ + 7

Implement power

Given an integer base x and a non-negative integer exponent n, compute xn modulo 10⁹ + 7 — the value wraps under a large prime modulus so the true (potentially astronomically large) result always fits in an ordinary integer. Repeated multiplication works but costs one multiplication per unit of n. Binary exponentiation reduces that to one squaring per bit of n — turning up to a billion multiplications into about thirty — by observing that xn can be built from x1, x2, x4, x8, ..., the doubling powers that correspond to each bit of n's binary representation.

Example 1:

Input: x = 2, n = 10

Output: 1024

Example 2:

Input: x = 2, n = 32

Output: 294967268

Example 3:

Input: x = -2, n = 3

Output: 999999999

+ 13 hidden test cases run on Submit.

Constraints:

  • -1000 ≤ x ≤ 1000
  • 0 ≤ n ≤ 10⁹
  • Return the answer modulo 10⁹ + 7

x =

2

n =

10