Table of Contents

ExplanationImplementation

Explanation

Let M=109+7M = 10^{9} + 7.

Fermat's Little Theorem tells us that ap11(modp)a^{p - 1} \equiv 1 \pmod{p}, so we can calculate abc(modM1)(modM)a^{b^c \pmod{M - 1}} \pmod{M} with modular exponentiation.

Implementation

Time Complexity: O(logP)\mathcal{O}(\log P)

MOD = int(1e9) + 7
for _ in range(int(input())):
a, b, c = map(int, input().split())
pow_bc = pow(b, c, MOD - 1)
print(pow(a, pow_bc, mod))

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!