We need to compute (X^Y) mod MOD where MOD = 1e9+7, but X and Y can be huge numbers up to 10^1000000 digits? Actually the constraint says: "1 ≤ X,Y ≤ 10^1000000" meaning they are numbers that can have up to 1,000,000 digits. That is extremely large, far beyond typical integer range. So we need to compute modular exponentiation with huge exponent and base modulo MOD. However, note: X is also huge, but we can reduce X modulo MOD first because we need (X mod MOD)^Y mod MOD. But Y is also huge, we need to compute exponent Y mod (MOD-1) if MOD is prime (since MOD = 1e9+7 is prime) due to Fermat's little theorem: For any a not divisible by MOD, a^(MOD-1) ≡ 1 mod MOD. So a^Y mod MOD = a^(Y mod (MOD-1)) mod MOD, but careful: if a is divisible by MOD, then a mod MOD = 0, so result is 0 unless exponent is 0 which is not the case. But also if a mod MOD = 0, then result is 0. For non-zero a, we can reduce exponent modulo MOD-1. However, need to handle the case when a mod MOD = 0 separately, but then the answer is 0 for Y>0. For Y=0? But Y≥1, so fine.
So the steps: For each test case, read X and Y as strings. Compute X_mod = X % MOD (since MOD fits in 64-bit). Compute exponent e = Y % (MOD-1). But we need to ensure that if a mod MOD = 0, answer is 0. But also, if a mod MOD = 0 and Y = 0? Not possible.
But there's a nuance: For a mod MOD = 0, if Y = 0 then result = 1, but Y≥1 so fine.
Additionally, there is a subtlety: When we reduce exponent modulo MOD-1, we need to consider that exponent could be extremely large, so we compute Y_mod = Y % (MOD-1) using modular reduction of the big integer string.
Also, note: If a mod MOD != 0, we can compute a^(Y_mod) mod MOD using fast exponentiation. But if Y_mod == 0 and a mod MOD != 0, then a^0 = 1.
However, careful: Because of Fermat: a^(p-1) ≡ 1 mod p for a not divisible by p. So indeed we can reduce Y modulo (p-1). But there's a catch: If a mod p = 0, then a^e mod p = 0 for any e > 0, so we don't need exponent reduction.
So algorithm per test case:
- Read strings X_str, Y_str.
- MOD = 1000000007
- Compute x = large_mod(X_str, MOD) -> an integer in [0, MOD-1].
- If x == 0: output 0 and continue.
- Compute phi = MOD - 1 = 1000000006.
- Compute y_mod = large_mod(Y_str, phi). (Note: if Y_str is huge, we compute modulo phi using string iteration.)
- Compute pow_mod(x, y_mod, MOD) using fast exponentiation.
But is it always correct? For exponent reduction, we rely on a^(k * (p-1) + r) ≡ a^r mod p. This holds if a is not divisible by p. However, it also holds if a ≡ 0 mod p, but then the result is 0, and we already handled that. So it's fine.
Edge case: What if Y is extremely large, but Y_mod == 0? Then we output a^(phi) mod MOD? Actually we compute pow(x, 0) = 1. But according to a^(k*(p-1)) ≡ 1 mod p (since a not divisible by p). So that's correct because a^(p-1) ≡ 1. So a^(k*(p-1)) ≡ 1^k = 1. So result 1 is correct. But wait: a^0 = 1, so if Y_mod = 0, it gives 1. So yes.
Now, large_mod function: For a number represented as a decimal string, we can compute modulo by iterating digits: res = 0; for each digit d: res = (res*10 + (d - '0')) % mod. Since mod fits in 32-bit, but we are using Python, we can just use Python's big integers; but the input numbers could be up to 1e6 digits, and Python can handle such big integers but might be memory/time intensive? Actually Python's big integers can handle up to millions of digits, but memory for each string is fine, but converting to int using int(s) would be extremely heavy because it would parse the entire string and allocate a big integer with millions of bits, which is about 10^1000000 bits? Wait: That's not bits but decimal digits. A number with 1 million decimal digits is about 3.3 million bits? Actually each decimal digit is about log2(10) ≈ 3.3219 bits, so 1 million decimal digits is about 3.32 million bits, which is about 0.4 MB? Actually 3.32 million bits = 415 KB? Wait: 3,321,928 bits = 415,241 bytes ~ 405 KB. That's not huge. But Python's int representation uses 30-bit digits or 15-bit? Actually CPython uses 30-bit digits on 64-bit builds. For a number with 3.32 million bits, it would require about 111k digits (since each digit holds 30 bits). That would be ~ 111k * (size of digit) maybe ~ 1 MB. That's okay. However, there might be up to 10 test cases, each with numbers up to 1 million digits. That's 10 million digits total, around 10 MB of input. Converting to int for each test case might be acceptable in Python? But we need to be careful about time: converting a string of 1 million digits to an int is O(n^2) maybe? Actually Python's algorithm for converting a decimal string to an int uses a combination; it might be O(n^2) in worst case? Typically conversion of huge decimal strings is not super fast, but might still pass if constraints are modest? However, since the problem mentions numbers up to 1000000 digits, and we need to handle modulo only, it's more efficient to compute modulo directly without building the full int.
But we can also use Python's built-in modulo on big ints after conversion; that would compute the mod using the big int, which might be efficient enough because the big int multiplication by 10 and addition is done in C. However, the conversion from string to int might allocate a huge int and then we compute modulus by using the int % mod, which might be fine. Let's evaluate: For a string of length 1e6, int(s) will parse and create a huge integer. That parsing in CPython uses a base conversion algorithm that is O(n^2) maybe? Actually typical algorithm: for each digit, do result = result * 10 + digit. This is O(n^2) in the number of digits because the multiplication result becomes large and takes time proportional to its size. But CPython might use a more efficient algorithm for large strings (like using base 10^9 etc.) I'm not entirely sure. In CPython, int(s) for huge decimal strings uses a divide and conquer algorithm with quadratic complexity? I recall that converting a long decimal string to int can be quite time-consuming; however, for 1 million digits, it might take a few seconds. But we have up to 10 test cases, so that might be too slow. However, we can do modulo while reading the string directly, which avoids constructing the big int. That would be O(n) per test case, which is safe.
But Python's input reading for huge strings also matters. However, typical online judges might have input with huge numbers, and they expect you to process as strings.
We'll implement large_mod(s, mod) that iterates through characters.
However, we need to compute Y mod (MOD-1) where MOD-1 = 1000000006 (10 digits). That's fine.
Now, for exponentiation: pow(x, y_mod, MOD) can be used, which is built-in modular exponentiation.
So final solution:
- Read integer T.
- For each test case, read line, split into two strings.
- Compute x_mod = mod_str(X, MOD)
- If x_mod == 0: print(0); continue.
- phi = MOD - 1
- y_mod = mod_str(Y, phi)
- ans = pow(x_mod, y_mod, MOD)
- print(ans)
Check sample:
Case 1: X=2, Y=2 -> x_mod=2, y_mod=2 -> 2^2=4 mod 1e9+7=4.
Case 2: X=3, Y=4 -> x_mod=3, y_mod=4 -> 3^4=81.
Case 3: X=250000000, Y=4 -> x_mod=250000000, y_mod=4 -> compute 250000000^4 mod 1e9+7. 250000000 = 25e7 = 25 * 10^7? Actually 250000000 = 2.5e8. Let's compute: 250000000^2 = 62500000000000000? Actually we can compute modulo: (250000000)^2 mod MOD =? But sample output is 660156264. So it matches.
We should also consider if Y is 0? Not in constraints, but if Y=0 then price on day 0? The problem says Y days to think, and price on Yth day. Starting day-1 price is X. So day-1 is the first day? Actually "On Day-1 his price is X. As each day passes the price becomes X times the price on previous day. Coach wants Y days to think before buying him." So I think day numbering: Day 1: price = X. Day 2: price = X * previous = X * X = X^2? Or X times previous? Wait: The description: "On Day-1 his price is 'X'. As each day passes the price of Messi becomes 'X' times the price on previous day. Coach wants 'Y' days to think before buying him." That could be interpreted that each day the price is multiplied by X, not by the previous price? Let's read carefully: "On Day-1 his price is 'X'. As each day passes the price of Messi becomes 'X' times the price on previous day." Actually ambiguous: It might mean that on day 2, the price is X * (price on day1) = X * X = X^2. So on day Y, price = X^Y. That matches sample: X=2, Y=2 -> 4 = 2^2; X=3, Y=4 -> 81 = 3^4; X=250000000, Y=4 -> (250M)^4. So yes, price on Yth day = X^Y. So exponentiation is correct.
Now, huge inputs: we need to be careful about reading lines with huge numbers. Using input() in Python might read the whole line. For 1e6 digits, it's fine but we need to ensure no extra whitespace. But there's a catch: The numbers may be given without spaces? The input says two space separated integers. So the line might be something like "123456... 789012...". Both numbers could be up to 1e6 digits, so the line length could be up to 2e6+1 characters. That's okay for Python's input() as long as memory is fine. However, on some judges, reading such a long line might be slow but acceptable. Alternative is to read from sys.stdin buffer directly.
We can use sys.stdin.readline(). However, we must be careful about trailing newline.
Implementation plan:
import sys
MOD = 1000000007
PHI = MOD - 1
def mod_str(s, m):
res = 0
for ch in s:
res = (res * 10 + (ord(ch) - 48)) % m
return res
def solve():
import sys
data = sys.stdin.read().split()
# After reading, data contains all tokens: first T, then alternating X and Y.
# However, if numbers are huge, splitting by whitespace might still create huge strings but that's fine.
# But we must be careful about memory: reading all input at once for T up to 10 and each number up to 1e6 digits -> total up to 20 million characters + T. That's ~20 MB, okay.
it = iter(data)
T = int(next(it))
out_lines = []
for _ in range(T):
X_str = next(it)
Y_str = next(it)
x_mod = mod_str(X_str, MOD)
if x_mod == 0:
out_lines.append('0')
continue
y_mod = mod_str(Y_str, PHI)
ans = pow(x_mod, y_mod, MOD)
out_lines.append(str(ans))
sys.stdout.write('\n'.join(out_lines))
if name == "main":
solve()
But one nuance: When we compute y_mod = mod_str(Y_str, PHI), and then use pow(x_mod, y_mod, MOD), what if y_mod = 0? Then pow(x_mod, 0, MOD) returns 1, which matches x_mod^(k*phi) mod MOD = 1. So correct.
But what if x_mod != 0 and Y = 0? Not possible.
Now, we must also handle potential overflow? In Python, integers are arbitrary precision, but we are doing mod at each step, so safe.
But note: The exponent we compute y_mod is modulo phi, but we should be careful: For exponent reduction, we assume a and MOD are coprime. But if a is not coprime, i.e., a mod MOD is not 0 but shares a factor with MOD, then Fermat's little theorem does not apply directly. However, we are using Euler's theorem: a^phi(MOD) ≡ 1 mod MOD if gcd(a, MOD)=1. Since MOD is prime, phi = MOD-1. For a not divisible by MOD (i.e., a mod MOD != 0), gcd(a, MOD)=1 because MOD is prime. So it's always coprime. So correct.
Now, potential pitfalls: The exponent Y may be extremely large, but we are reducing modulo phi, which is 1e9+6. But is there any case where Y is exactly 0? No.
Thus solution seems straightforward.
Now, check with sample:
Input:
3
2 2
3 4
250000000 4
Processing:
T=3.
Case1: X="2", Y="2": x_mod=2, y_mod=2 => 2^2 mod 1e9+7=4.
Case2: X="3", Y="4": x_mod=3, y_mod=4 => 3^4=81.
Case3: X="250000000", Y="4": x_mod=250000000, y_mod=4 => pow(250000000,4,MOD). Let's compute manually: 250000000^2 = 62500000000000000 = 6.25e16? Actually we can compute modulo: 250000000 mod MOD = 250000000. Squaring: (250000000^2) mod MOD = (25e7)^2 = 625e14? Better compute using Python: 250000000**2 = 62500000000000000, now mod 1000000007: 62500000000000000 % 1000000007 = ? We can compute stepwise: But sample output is 660156264. Let's trust.
Now, verify with code later.
Edge Cases:
- X huge, Y huge. For example X is a multiple of MOD: X_mod = 0 => answer 0.
- X not multiple, Y huge, Y_mod maybe 0 => answer 1. That would be the case if Y is a multiple of MOD-1. e.g., X=2, Y=1000000006 => answer 1? Actually 2^(1000000006) mod MOD = 1. So output 1.
Now, verify with code: need to make sure that pow(x_mod, y_mod, MOD) is correct.
Now, regarding large_mod function: It iterates over each character, computing modulo. For a string of length 1e6, it does 1e6 operations per number, which is fine. Python can handle maybe up to 10 million operations easily.
But we have two mod operations per test case (X and Y). So at most 20 million operations, which is okay.
Now, careful about newline characters: Using sys.stdin.read().split() will split on whitespace, which will treat each token as a separate string. For huge numbers, that's okay. However, note that if numbers contain leading zeros? The problem says integers, can have leading zeros? Probably not, but even if they do, our mod_str works fine.
But be aware that the input may have huge numbers that exceed memory if we read all at once? Up to 20 million characters (20 MB) is fine.
Alternatively, we could process line by line: for _ in range(T): line = sys.stdin.readline().strip().split() yields two strings, but reading line with huge numbers may also be fine. But using read().split() is simpler.
But one nuance: The first line of input contains T. After reading all tokens, the first token is T. However, if T is read as string and then converted to int, that's fine.
Now, implement and test.
Testing with sample.
Potential pitfalls: When using sys.stdin.read(), it loads entire input into memory. For T=10 and each number 1e6 digits, total about 20 million characters + overhead, maybe ~20 MB, which should be okay. However, some judges may have memory limits but typically 256 MB or more, so fine.
But there is a catch: The problem says "NOTE : Some test files contains numbers upto 100000