Fast Exponentiation

Compute A raised to the power X modulo 1,000,000,007, where A and X can be up to 10^18.

Medium4MathBit manipulationDivide and conquerInterviewNo attempts yetTime limit1sMemory limit512 MB

Problem

Given two integers AA and XX, think about how to compute AXA^X. The value can be enormous, so compute it modulo 109+710^9 + 7. Writing amodxa \bmod x for the remainder of aa divided by xx,

(a×b)modx={(amodx)×(bmodx)}modx(a \times b) \bmod x = \{(a \bmod x) \times (b \bmod x)\} \bmod x

holds. The remainders of two integers modulo 1,000,000,007 are therefore enough to compute the remainder of their product.

So multiplying AA by itself XX times looks like it would work. When XX grows into the range of a 64 bit integer, multiplying one factor at a time takes far too long to finish. Reduce the number of multiplications like this.

  1. Compute A1,A2,A4,A8,A^1, A^2, A^4, A^8, \dots in order. Each number is the square of the previous one, and stopping as soon as the exponent would pass XX is enough. Since XX stays inside a 64 bit integer, fewer than 64 numbers are computed.
  2. Now write XX in binary. For X=11X = 11, 11=1+2+811 = 1 + 2 + 8, and by the law of exponents A11=A1+2+8=A1×A2×A8A^{11} = A^{1+2+8} = A^1 \times A^2 \times A^8. Multiplying a few of the numbers from step 1 gives AXA^X.

Multiplying one factor at a time costs time proportional to XX, while the method above costs time proportional to logX\log X. Write a program that computes AXA^X modulo 109+710^9 + 7.

Input

The first line contains an integer AA. (1A10181 \le A \le 10^{18})

The second line contains an integer XX. (1X10181 \le X \le 10^{18})

Output

Print AXA^X modulo 1,000,000,007 on one line.