We are asked: given t test cases, each with m and n. Define:
a = (nC0)^2 + (nC1)^2 + ... + (nCn)^2
b = m
We need to compute a^b mod (10^9+6). Wait, the problem statement says "print modulo 10^9+6". Usually modulo is 10^9+7, but here it's 10^9+6. Let's verify: It says modulo 10^9+6. That's 1,000,000,006. That is a prime? 10^9+7 is prime, but 10^9+6 is 2 * 3 * 9, 10^9+6 = 1000000006 = 2 * 500000003. 500000003 is prime? I'm not sure. Actually 10^9+7 is prime; 10^9+6 is even, so not prime. But the problem explicitly says 10^9+6. Might be a typo? But sample: Input:
4
1 2
1 1
2 1
2 3
Outputs:
6
2
4
400
Let's test: For n=2, a = (2C0)^2 + (2C1)^2 + (2C2)^2 = 1^2 + 2^2 + 1^2 = 1+4+1=6. b=1 => 6^1 =6 mod 1000000006 =6. matches.
n=1: a = (1C0)^2+(1C1)^2 =1+1=2, b=1 =>2 =>2. Good.
n=1, m=2: a=2, b=2 => 2^2=4 mod =>4. matches.
n=3, m=2: a = sum_{k=0}^3 (3Ck)^2 = 1^2+3^2+3^2+1^2 =1+9+9+1=20. b=2 => 20^2=400. mod 1000000006 =400. matches.
So indeed they want a^b mod (10^9+6). Since modulus is not prime, careful: exponentiation can be done modulo that modulus using fast exponentiation. But a might be large: sum of squares of binomial coefficients: known combinatorial identity: sum_{k=0}^n C(n,k)^2 = C(2n, n). So a = C(2n, n). Yes, that's a well-known identity: the sum of squares of binomial coefficients equals the central binomial coefficient.
Thus a = C(2n, n). So we need to compute C(2n, n) mod M where M = 1000000006, then raise to power m modulo M.
So we need to compute C(2n, n) mod M for n up to 10^5, so 2n up to 200,000. But modulus is 1000000006 which is composite (2 * 500000003). That complicates modular inverses if we want to compute binomial coefficient modulo composite where n is relatively small. However, we can compute C(2n, n) as an integer (exact) because 2n <= 200,000, and the value might be huge but we can compute it modulo M directly by using multiplication and division modulo M. But division modulo a composite may not be invertible if divisor is not coprime to modulus. So we need to compute C(2n, n) modulo M, where M is 1000000006, which is composite. The straightforward approach: compute C(2n, n) as integer using exact arithmetic (since 2n <= 200,000, the binomial coefficient fits within? Actually C(200000,100000) is astronomically huge, far beyond typical integer range (it has about 60104 digits). So cannot compute exactly using built-in integer types (like Python arbitrary integers could handle it, but in C++ it would be too big). But we are to implement in some language? The problem statement doesn't specify language; we can assume we need to produce code. However, typical programming contests allow languages like C++, Java, Python. Since constraints are moderate, we could compute C(2n, n) modulo M by using prime factorization and Chinese remainder theorem? But M = 1000000006 = 2 * 500000003. 500000003 is prime? Let's check: 500000003. I can test: It is known that 500000003 is prime? I recall that 10^9+7 is prime; 10^9+6 is even; 10^9+5 divisible by 5; 10^9+3? Not sure. 500000003, is it prime? I think I've seen that 500000003 is prime, but not sure. Let's quickly verify: sqrt(500000003) is about 22360.9? Actually sqrt(5e8) ~22360? Wait 5e8 is 500 million, sqrt is about 22361? No, 22361^2 = about 500 million? 22361^2 = 2236122361 = 22361^2 = (22000+361)^2 approx = 484,000,000 + ... Let's compute properly: 22361^2 = (22000+361)^2 = 22000^2 + 222000361 + 361^2 = 484,000,000 + 15,884,000 + 130,321 = 500,014,321, which exceeds 500,000,003. So sqrt is about 22361? Actually 22360^2 =? 22360^2 = (223610)^2 = 2236^2100 =? Let's compute: 22360^2 = (223610)^2 = 2236^2 * 100. 2236^2 =? 2200^2=4,840,000; 2220036=158,400; 36^2=1296 => sum 5,000, 4,840,000+158,400=4,998,400 +1296 =4,999,696. Times 100 = 499,969,600. That's a bit less than 500,000,003. 22361^2 = (22360+1)^2 = 22360^2 + 222360 + 1 = 499,969,600 + 44,720 + 1 = 500,014,321 > 500,000,003. So sqrt is around 22360. So we need to check primes up to 22360. That's doable manually? We can suspect 500000003 is prime because it's often used in problems as a prime? Actually I recall that 10^9+6 is sometimes used in problems where modulus is not prime for some reason. But let's quickly test small primes: divisible by 3? sum of digits 5+0+0+0+0+0+0+0+3 = 8, not divisible by 3. 5? last digit 3, no. 7? 500000003 /7 = 71428571.857... 771428571 = 500000000 -? 7*71428571= 500,000, -? Let's compute: 71,428,571 7 = 500,000,000 -? Actually 71,428,5717 = 499,999,997, remainder 6 => not divisible. 11? Alternating sum: 5-0+0-0+0-0+0-0+3 = 8? Actually digits: 5,0,0,0,0,0,0,0,3 -> sum alternating? For 11: (5+0+0+0+0) - (0+0+0+3) =5 -3 =2, not multiple of 11. So not divisible by 11. 13? Hard. But for contest problems, they often set modulus prime, but here they wrote 10^9+6. However, note that the sample mod 10^9+6 gives 400, which is less than modulus, so no issue. But to compute C(2n, n) mod M, we might need to compute it modulo a composite. But if we just compute C(2n, n) as an integer using big integers (Python), we can compute the exact binomial coefficient using multiplicative formula and reduce mod M, but the intermediate numbers could be huge (like 200k! huge). But Python can handle big integers for factorials up to 200k? 200k! is astronomically huge with millions of digits. That would be too large in memory/time. We need a better method.
We can compute C(2n, n) mod M without computing huge numbers by using Lucas? No, M is composite. But maybe we can compute a directly as C(2n, n) and then exponentiate mod M. Since n ≤ 10^5, C(2n, n) can be computed modulo M by using prime factorization of M and Chinese Remainder Theorem (CRT). Since M = 2 * 500000003, if 500000003 is prime, then we can compute C(2n, n) mod each prime factor, then combine via CRT. Or we can compute using multiplicative formula with modular inverses for prime moduli, then combine.
But wait: modulus is 10^9+6 = 1000000006. Let's factor it: 1000000006 / 2 = 500000003. Is 500000003 prime? Let's check quickly with some known facts: 500000003 = 5e8+3. I recall that 500000003 is indeed prime? I'm not 100% sure. Quick search memory: I've seen some Codeforces problems where modulus is 10^9+6 and they treat it as not prime. But many solutions just compute binomial coefficient modulo 10^9+6 by using the fact that 10^9+6 is not prime but n is small enough to compute directly using multiplicative formula with cancellations? Possibly they compute the binomial coefficient as integer using precomputed factorials and modular inverses but if the modulus is not prime, inverses don't exist for numbers not coprime to modulus. However, we can avoid division by using prime factorization and subtracting exponents. Since n ≤ 100k, 2n ≤ 200k, the primes dividing the denominator might include factors that are not coprime to modulus. The modulus has prime factors 2 and 500000003. The denominator of C(2n, n) is n! * n!. It may contain many factors of 2, and maybe also factors of 500000003 if n is large enough (≥500000003? but n ≤ 10^5, so n is far less than 500000003, so n! does not contain any factor of 500000003. Since 500000003 > 200k, the prime 500000003 does not appear in any factorial up to 200k. So the only common factor between denominator and modulus is 2. That's crucial. Because the modulus M has factor 2. When we compute C(2n, n) mod M, the denominator may have factors of 2, which are not invertible modulo 2, but modulo the composite M, we need to handle the factor 2 specially.
We can compute C(2n, n) modulo M by computing it modulo 2 and modulo 500000003 (if prime) and then combine via CRT. But since 500000003 > 200k, computing modulo that prime is easy using factorials and Fermat's little theorem for inverses. For modulo 2, C(2n, n) mod 2: There's known result: C(2n, n) mod 2 is 1 if n is a power of 2? Actually by Lucas theorem, C(2n, n) mod 2 is 1 iff n has no carries when adding n+n in binary? More precisely, C(2n, n) mod 2 = 1 if n is of the form 2^k - 1? Let's derive: Lucas for mod 2: Represent n in binary, then C(2n, n) mod 2 = product over bits of C(bit of 2n, bit of n). But 2n in binary is n shifted left by one. So its bits are: n's binary digits with a trailing 0. Then C(0,0)=1, C(1,0)=1? Actually we need to compute C( (2n)_i, n_i ) for each bit i. For bit i, (2n)_i = (n shifted) so (2n)i = n{i-1}? Let's define index properly. Alternatively, known result: C(2n, n) is even for all n>0? Let's test small: n=1: C(2,1)=2 -> even. n=2: C(4,2)=6 -> even? 6 is even. n=3: C(6,3)=20 -> even. n=4: C(8,4)=70 -> even. n=5: C(10,5)=252 -> even. So seems C(2n, n) is even for n>=1? Actually check n=0: C(0,0)=1 odd. But for n>=1, it appears even. Is it always even? Known fact: The central binomial coefficient C(2n, n) is divisible by n+1 (Catalan number) but parity: C(2n, n) is even for n>=1. Indeed, it's known that C(2n, n) is even for all n>=1. Because the number of factors of 2 in C(2n, n) is the number of carries when adding n+n in base 2, which is at least 1 for n>0. So modulo 2, C(2n, n) ≡ 0 (mod 2) for n>=1. So a mod 2 = 0 for n>=1. For n=0? n is at least 1 by constraints (1 ≤ n). So a is even, thus a mod 2 = 0. So a ≡ 0 (mod 2). However, when we compute a mod M = 1000000006, which is even, a mod 2 part is just a mod 2, which is 0. So in the CRT, the solution modulo 2 is 0. The other modulus is 500000003, which is odd. So we can compute a mod 500000003 using standard modular arithmetic with inverses (since denominator's factors are all coprime to 500000003, as n! does not contain that prime). Then combine with the mod 2 result to get a mod (2 * 500000003) = M.
But careful: When combining with CRT, we have:
We want x such that:
x ≡ a2 (mod 2), where a2 = C(2n, n) mod 2 = 0 for n>=1.
x ≡ aP (mod P), where P = 500000003.
Since 2 and P are coprime (P is odd), we can combine. The solution is x = a2 + 2 * (( (aP - a2) * inv(2 mod P) ) mod P). But a2 = 0, so x = 2 * ( (aP * inv(2) mod P) )? Wait: Actually standard CRT:
x ≡ 0 (mod 2)
x ≡ aP (mod P)
We need integer x. Write x = 2k. Then 2k ≡ aP (mod P) => k ≡ aP * inv(2) (mod P). So k = (aP * inv2) mod P, where inv2 is modulo P. Then x = 2 * k + maybe add 2P? Actually x = 2*k. But that x may not be modulo M? The solution modulo M = 2P is unique: x = 2 * ( (aP * inv2) mod P ). However, we must ensure that x is between 0 and 2P-1. That works. But note that we need x modulo M. So we can compute:
inv2_modP = modular inverse of 2 modulo P. Since P is odd, 2 and P are coprime, inv2 exists.
Then a_mod_M = (2 * ((aP * inv2_modP) % P)) % M.
But wait, we must double-check: x = 2k. Given x ≡ aP (mod P), substituting: 2k ≡ aP (mod P). Multiply both sides by inv2: k ≡ aP * inv2 (mod P). So k = aP * inv2 (mod P). So x = 2 * (aP * inv2 mod P). This gives a value less than 2P. But is this congruent to 0 mod 2? Obviously yes. So that's the CRT solution.
But is it correct for all aP? Let's test with small example: Suppose M = 23=6, P=3. a2=0, aP= some. Then x = 2(aP * inv2 mod 3). inv2 mod 3 = 2 (since 22=4≡1 mod3). Then if aP=1, x = 2(12 mod3)=22=4 mod6 => 4. Check: 4 mod2=0, mod3=1? 4 mod3=1 correct. If aP=2, x=2*(22 mod3)=2(4 mod3=1)=2. 2 mod2=0, mod3=2? 2 mod3=2 correct. If aP=0, x=0. So works.
Thus we can compute a = C(2n, n) mod M via:
Compute aP = C(2n, n) mod P, where P = 500000003 (assumed prime). Then compute a_mod_M = (2 * ((aP * inv2_modP) % P)) % M.
But we also need to consider the case n=0? Not needed (n>=1). Also ensure that C(2n, n) is even, so a2=0; that holds for n>=1. Good.
Now, we need to compute C(2n, n) mod P, where P is prime and > 200k. Since P > 2n, we can compute factorials mod P up to 2n (max 200k) and use modular inverses. So that is easy.
So the plan:
For each test case:
- Input m, n.
- Compute a = C(2n, n) mod MOD, where MOD = 1000000006.
- Compute result = a^m mod MOD.
But careful: the exponent b = m, which can be up to 1e5. So exponentiation mod MOD straightforward.
We need to compute a efficiently for up to t=1000 test cases, n up to 1e5. So we can precompute factorials modulo P (500000003) up to maxN = 2*1e5 = 200000. Also precompute inverse factorials mod P. Since P is prime and >200000, we can compute factorials and inv factorials mod P. Then for each test case, aP = fact[2n] * invfact[n] % P * invfact[n] % P.
Then compute a_mod_M via CRT as above. But we also need to compute inv2 modulo P. Since P is odd, inv2 = (P+1)//2? Actually (P+1)/2 is integer if P is odd? For any odd prime, 2^{-1} mod P = (P+1)/2 because 2 * (P+1)/2 = P+1 ≡ 1 mod P. So inv2 = (P+1)//2 = (500000003+1)/2 = 250000002? Wait 500000004/2 = 250000002. Yes.
Thus a_mod_M = (2 * ((aP * inv2) % P)) % MOD.
But is that correct? Let's test with sample: n=2: Compute aP = C(4,2)=6 mod P. Since P=500000003, aP=6. inv2=250000002. Compute (6 * 250000002) % P = ? 6*250000002 = 1,500,000,012. Mod 500000003: 500000003 3 = 1,500,000,009. Remainder = 3. So (aP * inv2) mod P = 3. Then 23=6. a_mod_M=6. Good.
n=1: aP=2. 2inv2 mod P = 2250000002=500000004 mod P = 1? Actually 500000004 mod 500000003 = 1. times 2 =2. Good.
n=3: aP=20. 20inv2 mod P =20250000002=5,000,000,040. Compute mod P: 50000000310=5,000,000,030. remainder=10. 210=20. So a_mod_M=20.
Works.
Now, we also need to compute a^b mod MOD. Since a is computed mod MOD, we can do fast exponentiation. However, note that MOD is composite, but exponentiation modulo composite works fine using multiplication mod MOD.
But wait: Could there be any subtlety because a might be divisible by MOD? That would make a mod MOD 0, then 0^b = 0 mod MOD for b>0. But is it possible that a is a multiple of MOD? a = C(2n, n). For n up to 1e5, a is less than MOD? C(200000,100000) is huge, definitely > MOD (10^9+6). So a mod MOD may be 0. That's fine. But we must ensure that our method of computing a_mod_M via CRT yields the correct remainder modulo M, even when a is divisible by M. Since a may be divisible by M, a_mod_M should be 0. Our CRT method should produce 0 if aP = 0 mod P? Let's check: a_mod_M = 2 * ((aP * inv2) % P) mod M. For a_mod_M to be 0 mod M, we need (aP * inv2) % P = 0, i.e., aP ≡ 0 mod P. That means C(2n, n) mod P = 0. Since P > 2n, that only happens if P divides C(2n, n). Since P is >2n, it cannot divide any factor in numerator unless it appears from combination, but P is larger than all numbers up to 2n, so C(2n, n) = (2n)!/(n! n!). The prime P does not divide any factorial because all numbers < P. So P cannot divide denominator or numerator (except if some factor equals P, but P > 2n, so no). So C(2n, n) mod P is nonzero. Thus a_mod_M cannot be 0? That can't be right because C(2n, n) can be a multiple of M, and M = 2 * P, and we argued that P does not divide C(2n, n) because P > 2n, so the only way for M to divide C(2n, n) is for the factor 2 to appear with at least exponent 1 (which it does) but also P would need to divide it, which it doesn't. So C(2n, n) cannot be a multiple of M because M includes prime factor P > 2n. For a number to be divisible by P, it must have a factor of P. Since C(2n, n) is product of numbers up to 2n divided by factorials, none of which are P, it cannot have prime factor P. Therefore a_mod_M cannot be 0; it will be some number less than M but not divisible by P. However, it could be divisible by 2. Indeed a is even, so a_mod_M is even (or could be 0 mod 2). But it cannot be 0 mod M because that would require P dividing a. So a_mod_M is non-zero. Good.
Thus our method seems safe.
Now we need to compute C(2n, n) mod P efficiently. Precompute factorials up to MAX = 200000 (since n up to 100k, 2n up to 200k). Also precompute inverse factorials using Fermat's little theorem: invfact[MAX] = fact[MAX]^(P-2) mod P, then downwards.
Complexities: t up to 1000, precomputation O(MAX) which is fine.
But we must confirm that P = 500000003 is indeed prime. Let's do a more thorough check. The number 500000003 is suspicious: it is 5e8+3. I recall that 500000003 is prime? Actually, I think 500000003 is prime. I could quickly check divisibility by small primes up to maybe 100. But to be absolutely sure, many contest problems use modulus 10^9+6, and they expect participants to factor it as 2 * 500000003, and treat 500000003 as prime. It's a known prime: 500000003 is indeed a prime number? A quick memory: The prime numbers around 500 million: I know that 500000003 is a prime because I've seen it used in problems. I can check: It is not divisible by 3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97... That seems plausible. Also, 500000003 is a prime according to some sources? Let me do some mental check: 500000003 mod 7 =? 771428571 = 499999997, remainder 6. Not divisible. mod 13: 1338461538 = 499999994? Actually 1338461538 = 1338,461,538 = 500,000, - let's compute: 38,461,538 10 = 384,615,380; 3 = 115,384,614; sum=500,000, - actually 384,615,380+115,384,614=500,000 - oh that sums to 500,000, - maybe exactly 500,000, -? 384,615,380+115,384,614 = 500,000 - wait I'm messing. Better: 13 * 38,461,538 = 500,000, - compute precisely: 38,461,538 * 13 = 38,461,538 * (10+3) = 384,615,380 + 115,384,614 = 500,000 - actually 384,615,380+115,384,614 = 500,000 - I get 500,000 -? 384,615,380+115,384,614 = 500,000 -? 384,615,380+115,384,614 = 500,000 -? 384,615,380+100,000,000=484,615,380; plus 15,384,614 = 500,000 -? 484,615,380+15,384,614=500,000 -? Actually 484,615,380+15,384,614 = 500,000 -? It's 500,000 -? Let's add: 484,615,380 + 15,384,614 = 500,000 -? 484,615,380+15,384,614 = 500,000 -? 484,615,380+15,384,614 = 500,000 -? I'll do column: 484,615,380 + 15,384,614 = (484,615,380+15,000,000)=499,615,380; +384,614=500,000 -? 499,615,380+384,614 = 500,000 -? 499,615,380+384,614 = 500,000 -? Actually 499,615,380+384,614 = 500,000 -? 499,615,380+384,620 = 500,000,000; but it's 384,614, so 500,000,000 - 6 = 499,999,994? That seems off. Let's compute accurately: 38,461,538 * 13 =? Compute 38,461,538 * 10 = 384,615,380. 3 = 115,384,614. Sum = 384,615,380 + 115,384,614 = 500,000 - oh I see: 384,615,380 + 115,384,614 = 500,000 -? Actually 384,615,380 + 115,384,614 = 500,000, - I'll add: 384,615,380 + 115,384,614 = (384,615,380+115,000,000)=499,615,380; +384,614 = 500,000 -? 499,615,380+384,614 = 500,000 -? 499,615,380+384,614 = 500,000 -? 500,000,000 - (500,000,000 - 499,615,380 - 384,614)? Let's just use modulo: 500,000,003 mod 13. 1338,461,538 =? 38,461,538 * 13 = 38,461,538 (10+3) = 384,615,380 + 115,384,614 = 500,000 - I think it's exactly 500,000, - maybe 500,000, -? I'll compute exactly using Python mentally? Actually 38,461,538 * 13 =? 38,461,538 * 12 = 461,538,456? This is getting messy. Instead, let's do simpler: 500,000,003 ÷ 13 = 38,461,538.692... Actually 1338,461,538 = 1338,000,000=494,000,000; 13461,538=6,000, - hmm.
Let's not waste time; it's highly likely that 500000003 is prime for contest purposes. I recall that 500000003 is prime, and many problems use 10^9+6 as modulus with factor 2*500000003 and treat the larger factor as prime. I'd proceed with that assumption.
Thus we can compute modulo P using factorials.
But we must also consider that modulus for final answer is MOD = 1000000006, not P. So after obtaining a_mod_M, we exponentiate modulo MOD.
Edge Cases: n=0? Not allowed (1 ≤ n). So fine.
Now, we need to implement this.
Potential pitfalls:
-
The exponent b = m, which can be up to 100000. So exponentiation fine.
-
t up to 1000, precomputation done once.
-
Since MOD = 1000000006 is even, but exponentiation with modulo even number is fine.
-
However, we need to ensure that when computing a_mod_M, we correctly handle potential overflow in intermediate multiplication. Use 64-bit (long long) with modulo operations. Since P is about 5e8, product of two such numbers can be up to ~2.5e17, which fits in 64-bit (max 9.22e18). So safe for C++ using unsigned long long or __int128 if needed. But careful: (aP * inv2) % P might be computed as (aP % P) * (inv2 % P) % P, each < P ~5e8, product < 2.5e17, which may exceed 64-bit? 2.5e17 < 9.22e18, so fits in unsigned 64-bit. In C++ unsigned long long max is 18446744073709551615 ~1.84e19, so fine. Then multiply by 2 gives < 5e17, still safe. But when we compute a_mod_M = (2 * temp) % MOD, temp < P, so 2temp < 1e9, < MOD? Actually MOD = 1e9+6, 2temp <= 2*(P-1) = about 1e9+? Actually P = 500000003, 2*(P-1)=1000000004, which is less than MOD (1000000006). So a_mod_M = 2 * temp (since temp < P, 2temp < 2P = MOD? Actually 2P = 1000000006, which is exactly MOD. But temp is modulo P, so temp can be from 0 to P-1. If temp = P-1, then 2(P-1) = 1000000004, which is less than MOD. If temp = 0, then 0. So a_mod_M = 2temp, and since 2temp < MOD, we don't need modulo? Actually MOD = 2P exactly. So 2temp ranges from 0 to 2P-2 = MOD-2. So it's always less than MOD, thus a_mod_M = 2temp directly. So we can compute a_mod_M = 2 * ((aP * inv2) % P). No need for additional mod.
But careful: When temp = (aP * inv2) % P, temp can be 0 if aP=0. As argued aP cannot be 0, but even if it were, 0 is fine.
Thus a_mod_M is simply 2 * (aP * inv2 % P). That's straightforward.
Now, we need to compute inv2 modulo P. Since P is prime, inv2 = (P+1)/2 = 250000002.
But note: P is 500000003, so (P+1)/2 = 250000002. Good.
Now, algorithm steps:
Precomputation:
- Set MOD = 1000000006, but we actually need MOD for final answer. But for binomial, we need P = 500000003.
- Determine maxN = max over test cases of n. Since t up to 1000, we can read all test cases first, or just precompute up to 100000 (or 200000). To be safe, compute factorials up to 200000.
- Precompute fact[i] modulo P for i = 0..200000.
- Precompute inv_fact[i] modulo P using Fermat: inv_fact[MAX] = pow_mod(fact[MAX], P-2, P), then downwards inv_fact[i-1] = inv_fact[i] * i % P.
Alternatively, precompute inverse of numbers 1..200000 modulo P using modular inverses, but factorial method is easier.
Then for each test case (m, n):
- aP = fact[2*n] * inv_fact[n] % P * inv_fact[n] % P.
- temp = aP * inv2 % P.
- a = 2 * temp. (This is a_mod_M)
- result = pow_mod(a, m, MOD). (Since MOD = 1000000006)
- Output result.
But wait: Need to ensure that exponentiation uses modulus MOD (1000000006). However, a is computed modulo MOD, and we exponentiate modulo MOD. That's correct.
Double-check with sample: Already tested.
Now consider n large, e.g., n=100000. Then 2n=200000. factorials up to 200000 modulo P should be fine.
Potential issues: In computing fact[2n] mod P, since P > 200000, no zero factors.
Now, verify that our CRT solution yields correct a_mod_M even when a_mod_2 is 0? We used CRT. But what about the case n=0? Not needed. So it's fine.
But is it always true that a_mod_2 = 0? For n>=1, C(2n, n) is even. That's a known fact: Because C(2n, n) = 2 * (2n-1 choose n-1)? Actually, C(2n, n) = 2 * C(2n-1, n-1). So indeed it's even. So a_mod_2 = 0. So good.
Thus the problem reduces to computing central binomial coefficient modulo a prime (the large factor) and then doubling it after multiplying by inverse of 2 mod that prime? Wait, we did 2 * (aP * inv2 mod P). That simplifies: 2 * (aP * inv2) mod (2P) = aP * (2 * inv2) mod 2P? But 2*inv2 ≡ 1 (mod P), but we are not mod P, we are mod 2P. Actually, note: If we have aP = a mod P, and we know a is even, then a mod (2P) is uniquely determined by aP: Because a = 2k, and 2k ≡ aP (mod P) => 2k = aP + tP => k = (aP + tP)/2. Since a is integer, k integer, so (aP + tP) must be even. Since aP mod ? Actually we solved using inv2 mod P. So our derived formula is correct.
Alternatively, we could compute a directly as C(2n, n) mod MOD by performing modular arithmetic with cancellations of factor 2, but CRT is cleaner.
Now, about computing inv2_modP: Since P is prime, we can compute pow(2, P-2, P). But (P+1)/2 is easier.
Now, final answer: a^m mod MOD. Since MOD is not prime, we cannot use Fermat for exponent reduction.
Now, test more:
If m=0? But constraints m ≥ 1, so ok.
Now, edge: n=100000, m=100000. Compute aP = C(200000,100000) mod P. This is some number. Then a = 2 * (aP * inv2 % P). Then compute a^m mod MOD. Since MOD is about 1e9, exponentiation can be done by fast exponentiation (log m) per test case, total t * log(m) <= 1000 * ~17 = 17000, fine.
But we need to be careful about large exponent and modulus composite. pow_mod works fine.
Now, confirm that a^b mod MOD is indeed what is required: "print modulo 10^9+6". So yes.
Now, implementation details in C++:
-
Use long long for modulus operations. Since MOD = 1000000006, which fits in int (but it's > 2^31? 1e9+6 < 2^31=2147483648, so fits in signed 32-bit, but multiplication may overflow 32-bit. So use long long.
-
Precompute factorial array up to 200000 modulo P.
-
Compute inv2 = (P+1)/2.
-
For each test case:
aP = fact[2n] * inv_fact[n] % P * inv_fact[n] % P;
temp = aP * inv2 % P;
a = (temp * 2) % MOD; // since 2temp < MOD, could just assign a = 2*temp.
result = pow_mod(a, m, MOD);
But wait, a = 2 * temp. Since temp < P, 2temp < 2P = MOD, so a = 2temp is already < MOD. So we can set a = 2 * temp.
But note: The result of aP * inv2 % P is temp in [0, P-1]. Multiply by 2 yields at most 2P-2 = MOD-2. So safe.
Now, what about potential overflow when computing 2 * temp? temp up to 5e8, 2*temp up to 1e9, fits in 32-bit signed int? 1e9 < 2^31, yes. So it's safe for int as well.
Now, we need to compute pow_mod for exponent m (max 1e5). Write standard fast exponentiation.
Now, is there any need to consider that MOD is even? Not an issue.
Now, test a random case: n=4, m=1: a = C(8,4)=70. Compute aP = 70 mod P = 70. temp = 70 * inv2 mod P: 70 * 250000002 = 17,500,000,140. Mod P: P 35 = 17,500,000,105? Actually 500,000,00335 = 17,500,000,105. Subtract: 17,500,000,140 - 17,500,000,105 = 35. So temp = 35. Then a = 2*35 = 70. Good.
Now, what about n=5: C(10,5)=252. aP=252. temp = 252inv2 mod P: 252250000002 = 63,000,000,504. P126 = 63,000,000,378? Actually 500,000,003126 = 500,000,003*100=50,000,000,300; *20=10,000,000,060; *6=3,000,000,018; sum=63,000,000,378. Remainder = 504-378=126? Actually 63,000,000,504 - 63,000,000,378 = 126. So temp=126, a=252. Works.
Thus the method is correct.
Now, is there any scenario where the CRT combination fails? If a_mod_2 was not 0, but we assumed it. For n>=1 it's 0. Good.
Now, about t up to 1000, precomputation up to 200,000 factorial mod P is fine.
Now, need to parse input and output.
Now, any pitfalls: The modulus for final answer is 10^9+6, which is 1000000006. But we used P = 500000003 and inv2 = (P+1)/2. But careful: (P+1)/2 = 250000002, correct. However, in C++ integer division, (P+1)/2 yields 250000002 exactly since P+1 = 500000004, half is 250000002. Good.
Now, we also need to compute inv_fact[200000]. We need modular exponent for the factorial. Compute fact[0]=1; for i=1..MAX: fact[i]=fact[i-1]*i % P.
Then compute inv_fact[MAX] = pow_mod(fact[MAX], P-2, P). Since MAX is up to 200k, that exponentiation is O(log P) ~ 30 steps. Then compute inv_fact[i-1] = inv_fact[i] * i % P for i down to 1.
Now, note: P is 500000003, which is about 5e8, but exponentiation mod P is okay.
Now, what about memory? arrays of size 200001 for fact and inv_fact, okay.
Now, we also need to store test cases? Not necessary; we can process on the fly after precomputation.
Edge Cases:
- n=0? Not allowed.
- m=0? Not allowed.
- Maybe n=1e5, m=1e5: we need to compute a^m mod MOD. Since a might be up to MOD-1 (~1e9), raising to power 1e5 can be done with exponentiation; time is fine.
Now, verification with another test: Suppose n=10, a = C(20,10)=184756. Compute a_mod_M: aP = 184756 mod P = 184756; temp = 184756inv2 mod P: since inv2 = 250000002, product = 184756250000002 = 46,189,000, ? Let's compute mod P: Since 184756 is even? Actually 184756 * inv2 mod P: Since inv2 is essentially (P+1)/2, which is equivalent to (1/2) mod P. So temp should be (aP / 2) mod P? Because multiplying by inv2 is division by 2 mod P. But careful: In modular arithmetic, aP * inv2 ≡ aP / 2 (mod P) if aP is even? But aP may be odd. However, aP = C(2n, n) mod P. Since actual C(2n, n) is even, but mod P it may be odd or even. For example, C(4,2)=6 is even, mod P 6 is even, dividing by 2 gives 3 mod P, which is temp. That matches. For C(8,4)=70, even, mod P 70 even, /2=35 mod P, matches. For C(10,5)=252, even, mod P 252 even, /2=126 mod P. So it appears that for these small n, aP is even. Is aP always even? Not necessarily, because modulo an odd prime, an even integer can be congruent to an odd number? Even integer mod odd prime could be odd if the integer is even but after subtracting multiples of the odd prime, the remainder could be odd. For example, consider even integer 2P+2 = 2 mod P? Actually 2P+2 ≡ 2 (mod P), which is even. 2P+4 ≡ 4 even. In fact, if integer is even, then its remainder modulo an odd modulus is even if the integer is a multiple of the modulus? Actually not: The parity of the remainder doesn't necessarily match the parity of the original number when modulus is odd because subtracting an odd multiple changes parity. For example, take P=7 (odd), even number 8 mod 7 = 1, which is odd. So an even number can have an odd remainder modulo an odd modulus. So aP = C(2n, n) mod P could be odd even though actual C(2n, n) is even. For instance, let's test with n=2, P=7: C(4,2)=6, mod7 =6 (even). n=3, C(6,3)=20 mod7 =6 (even). n=4, C(8,4)=70 mod7=70 mod7=0? Actually 710=70, so 0 (even). n=5, C(10,5)=252 mod7=252-736=252-252=0 (even). Not seeing odd. Let's try n=6, C(12,6)=924 mod7: 7132=924, remainder 0. n=7, C(14,7)=3432 mod7: 7490=3430, remainder 2 (even). Might always be even? Possibly due to property that C(2n,n) is even, and modulo an odd prime, parity may not be preserved but maybe there is a theorem: C(2n,n) mod p is even if p is odd? Not necessarily; example: p=5, C(4,2)=6 mod5=1 (odd). Wait! Check: C(4,2)=6, mod5=1 which is odd! So there is a counterexample. So aP can be odd. For P=5, but our P is 500000003, which is odd. So aP could be odd. Then what happens with our temp = aP * inv2 mod P? Since inv2 is (P+1)/2, this computes aP * (1/2) mod P. If aP is odd, then aP/2 is not an integer in usual sense, but mod P it's well-defined because 2 has an inverse. So temp will be some integer mod P. Then a = 2 * temp mod MOD. Does that produce the correct a_mod_M? Let's test with a small analogue: Let's take M = 2 * 5 =10 (instead of 2P). P=5. Suppose actual a = C(2n,n) with n=2: a=6. Then a mod M =6. Compute aP = a mod5 =1 (odd). inv2 mod5 = 3 because 23=6≡1 mod5. temp = 13 mod5 =3. a_mod_M = 23 =6 mod10 =6. Works.
Another: n=1, a=2, aP=2 mod5=2, inv2=3, temp=23=6 mod5=1, a_mod_M=21=2. Works.
n=3, a=20, aP=20 mod5=0, temp=0, a_mod_M=0? But actual a mod10 = 20 mod10=0. So works, but aP=0.
n=4, a=70, aP=70 mod5=0, a_mod_M=0? 70 mod10=0, works.
n=5, a=252, aP=252 mod5=2? 550=250, remainder 2, inv2=3, temp=23=6 mod5=1, a_mod_M=2, but 252 mod10 = 2. Yes.
So the formula works even when aP is odd. So no issue.
Thus our method is robust.
Now, we should also consider the possibility that P might not be prime. But it's likely prime. However, if P is not prime, then modular inverses may not exist for some numbers, but since we are only dealing with factorials up to 200k, and P > 200k, all numbers 1..200k are less than P and not multiples of P, so they are coprime to P regardless of primality. But we need to compute inverse factorials modulo P. That requires the existence of modular inverses of factorial numbers. Since factorial numbers are product of numbers coprime to P (as all numbers < P), they are coprime to P as well, so inverses exist modulo P if P is any integer coprime to these numbers. But we also need to compute inv_fact[MAX] via exponentiation: inv_fact[MAX] = fact[MAX]^{phi(P)-1} mod P if we know phi(P). If P is not prime, we don't know phi(P) easily, and we cannot simply use P-2 exponent. So primality is important for using Fermat's little theorem to compute the inverse of fact[MAX] efficiently. However, we could compute inverses of all numbers 1..MAX modulo P using extended Euclid, since numbers are small, but that would be O(MAX) and feasible. But we also need inv_fact, we could compute inv[i] for each i, then compute inv_fact as product. That's doable even if P is not prime, as long as gcd(i, P)=1. Since i <= 200k < P, and if P is not prime but could share a factor with i? If P is composite and has a prime factor ≤200k, then some i may not be coprime to P, and inverses would not exist. However, P = 500000003, even if it's composite, its prime factors could be less than or equal to sqrt(P) ≈ 22360, which is less than 200k. So it's possible that P has a small prime factor, say 5, 7, etc. Then some i would share that factor, and factorial would include that factor, making it not invertible modulo P. In that case, we cannot compute C(2n, n) mod P using factorials and inverses because the denominator may have factors that are not invertible modulo P. So we need to be sure P is prime, or else we need a different method.
Given the problem statement uses modulus 10^9+6, it's likely they expect participants to notice that a = C(2n,n) and to compute it modulo the given modulus. They might expect participants to use the fact that n ≤ 10^5, and that the modulus is 10^9+6, which is not prime, but they could compute C(2n, n) using normal multiplication and division with big integers? But as argued, C(200000,100000) is huge, but maybe they intend using prime factorization of numerator and denominator, then compute product modulo M, handling exponent of each prime including those dividing M. Since M has prime factors 2 and 500000003, and 500000003 > 200k, only factor 2 may cause trouble. So they could compute C(2n, n) as an integer modulo M by using the multiplicative formula and at each step do gcd with M to simplify? But that seems messy.
Alternatively, they might have intended the modulus to be 10^9+7, but sample uses 10^9+6? Check sample: 400 mod 10^9+7 is 400, mod 10^9+6 is also 400. So doesn't discriminate. Maybe they mistakenly wrote 6 instead of 7. In many problems, they use 10^9+7. Could be a typo. But the problem text says 10^9+6 twice? It says "print modulo 10^9+6". And in constraints they didn't specify a prime. Possibly it's deliberate.
Let's search memory: I recall a HackerEarth problem named "Little Stuart and Evil Hackers" or something. Actually, I think I've seen a problem: "Evil hackers are trying to hack the database..." It might be from HackerEarth. I can try to recall if modulus is 10^9+7 or 10^9+6. I'm not sure.
But given the constraints, the solution I derived using CRT on the factorization 2*500000003 is elegant. And many problems with modulus 10^9+6 rely on that factorization and use CRT. So I'll assume it's correct.
Now, I need to implement.
Implementation Steps in C++:
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int MAX_N = 200000; // maximum 2*n
- const ll MOD = 1000000006LL;
- const ll P = 500000003LL; // factor
- const ll inv2_modP = (P + 1) / 2; // 250000002
Precompute factorial and inv factorial:
-
vector fact(MAX_N+1), inv_fact(MAX_N+1);
-
fact[0]=1; for i=1..MAX_N: fact[i] = fact[i-1] * i % P;
-
inv_fact[MAX_N] = power_mod(fact[MAX_N], P-2, P); // using fast exponent
-
for i=MAX_N; i>=1; i--: inv_fact[i-1] = inv_fact[i] * i % P;
Power_mod function:
ll power_mod(ll a, ll b, ll mod) {
ll res = 1;
while(b) {
if(b&1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
Processing:
Read t.
Optionally, read all test cases to find max_n to limit precomputation, but we can precompute up to 200000 directly.
For each test case:
- read m, n.
- aP = fact[2*n] * inv_fact[n] % P * inv_fact[n] % P;
- temp = aP * inv2_modP % P;
- a = (2 * temp) % MOD; // though 2temp < MOD, so just a = 2temp.
- ans = power_mod(a, m, MOD);
- print ans.
Check with sample.
Now, confirm that for n=100000, fact[200000] fits in memory and computations are fine.
Potential overflow in fact[i] * i % P: fact[i-1] < P, i <= 200000, product < P * 200000 ~ 1e14, which fits in 64-bit (max ~9e18). So using long long (which is typically 64-bit on modern compilers) is safe if we do multiplication and then % P. But to avoid overflow we can cast to __int128 if needed, but likely not necessary as product is less than ~1e14, which is fine for 64-bit. Actually 5e8 * 2e5 = 1e14, yes within 9e18. So safe.
For power_mod, a may be up to MOD-1 ~1e9, squaring gives up to 1e18, still within 64-bit unsigned? signed long long max 9.22e18, so safe. Use (a * a) % mod.
Now, ensure that we use % MOD for final exponentiation. The base a is less than MOD, so fine.
Edge Cases: We should consider if a = 0 mod MOD? As argued, can't happen because P doesn't divide a. But if aP=0? That would happen if fact[2n] % P =0? Since P > 2n, fact[2n] is product of numbers < P, none divisible by P, so not zero. So aP !=0. So a is non-zero mod MOD.
Now, what about the possibility of P not being prime? As argued, it's essential that we can compute inv_fact[MAX_N] via exponentiation. If P is not prime, then using P-2 may not give the inverse. However, we could compute modular inverse using extended Euclid, but we need to compute inv_fact[MAX_N] which is the inverse of fact[MAX_N] modulo P. Since fact[MAX_N] and P are coprime (as all factors < P and if P had a small prime factor p <= 200k, then fact[MAX_N] would include p, making gcd not 1, so inverse wouldn't exist). So we must be sure that P is prime, or at least that P is coprime to all numbers up to 200k. Since P=500000003, if it were composite, its smallest prime factor would be at most sqrt(P) ≈ 22361, which is less than 200k, so there would exist numbers ≤200k that share that factor, and fact[MAX_N] would be divisible by that factor, thus not invertible modulo P. So the approach would fail. Therefore, the problem implicitly relies on P being prime. So I'm confident.
Now, let's double-check primality of 500000003. Let's quickly verify by checking divisibility by small primes up to 100. I can manually compute 500000003 mod some primes:
-
mod 2: odd.
-
mod 3: sum digits = 5+0+0+0+0+0+0+0+3 =8, not divisible by 3.
-
mod 5: last digit 3, not.
-
mod 7: 7*71428571=499999997, remainder 6.
-
mod 11: alternating sum: 5-0+0-0+0-0+0-0+3 = 8? Actually compute: digits from left: 5,0,0,0,0,0,0,0,3. Starting with + for most significant: +5 -0 +0 -0 +0 -0 +0 -0 +3 = 8? Wait, sum of digits in odd positions (from rightmost? Usually for divisibility by 11, you take alternating sum starting from rightmost. But easier: compute 500000003 mod 11. 1145454545 = 500000 -? Let's compute: 11 * 45454545 = 500,000, -? Actually 45,454,545 * 11 = 45,454,545 10 + 45,454,545 = 454,545,450 + 45,454,545 = 500,000 -? That's 500,000 -? 454,545,450+45,454,545 = 500,000, -? It's 500,000, -? Let's compute precisely: 45,454,545 * 11 = 45,454,545 * (10+1) = 454,545,450 + 45,454,545 = 500,000 -? Actually 454,545,450 + 45,454,545 = 500,000, -? The sum is 500,000 -? Let's add: 454,545,450 + 45,000,000 = 499,545,450; +454,545 = 500,000, -? 499,545,450 + 454,545 = 500,000, -? That's 500,000, -? I'm mixing. I'll compute mod 11 using known: 10 ≡ -1 mod 11. So 500,000,003 = 510^8 + 3. 10^2 ≡ 1? Actually 10 ≡ -1 mod 11 => 10^even ≡ 1, 10^odd ≡ -1. Since 8 is even, 10^8 ≡ 1 mod 11. So 510^8 ≡ 5 mod 11. Then +3 => 8 mod 11. So not divisible by 11.
-
mod 13: 10 ≡ -3 mod 13? Might use known: 10^6 ≡ 1 mod 13? Actually 1376923 = 999999, so 10^6 ≡ 1 mod 13? 10^6 = 1,000,000; 1376923 = 999,999, so 10^6 ≡ 1 mod 13. Then 10^8 = 10^2 * 10^6 ≡ 10^2 * 1 = 100 mod 13 ≡ 9 (since 137=91, remainder 9). So 510^8 ≡ 5*9=45 mod13 ≡ 45-39=6. +3 =>9 mod13. So not divisible.
-
mod 17: 10^8 mod 17? 10^2=100 mod17=15, 10^4=15^2=225 mod17=4 (since 1713=221, remainder 4), 10^8=4^2=16 mod17. So 516=80 mod17=80-68=12, +3=15 mod17, not 0.
-
mod 19: 10^2=100 mod19=5, 10^4=5^2=25 mod19=6, 10^8=6^2=36 mod19=17. 5*17=85 mod19=85-76=9, +3=12.
-
mod 23: 10^2=100 mod23=8, 10^4=8^2=64 mod23=64-46=18, 10^8=18^2=324 mod23=324-2314=324-322=2. 52=10, +3=13.
-
mod 29: 10 mod29=10, 10^2=100 mod29=13, 10^4=13^2=169 mod29=169-145=24, 10^8=24^2=576 mod29=576-2919=576-551=25. 525=125 mod29=125-116=9, +3=12.
-
mod 31: 10^2=100 mod31=7, 10^4=7^2=49 mod31=18, 10^8=18^2=324 mod31=324-310=14. 5*14=70 mod31=70-62=8, +3=11.
-
mod 37: 10^3=1000 mod37=1000-3727=1000-999=1, so 10^6 ≡1, 10^8=10^210^6≡1001=100 mod37=100-74=26. 526=130 mod37=130-111=19, +3=22.
-
mod 41: 10^5=100000 mod41? Actually known that 10^5 mod41= ? But I'll stop. It seems likely that 500000003 is prime, as many sources confirm that 500000003 is a prime number. I've seen it used in problems as prime. So we can assume.
Now, verify constraints: t up to 1000, n up to 100000. Precomputation O(200k). Each test case O(1) for a and O(log m) for exponentiation. Good.
But wait: There's a catch: The modulus for final answer is 10^9+6, which is 2 * 500000003. When we compute a^b mod MOD, we need to handle exponentiation. But if a is computed as 2*temp, and temp may be up to 500000002, then a may be as large as 1000000004. That's less than MOD. So fine.
Now, we should confirm that a^b mod MOD can be computed using fast exponentiation with multiplication modulo MOD. Since MOD fits in 32-bit, we can use 64-bit for multiplication.
Now, potential issue: In the CRT step, we used inv2_modP = (P+1)/2. That works because 2 and P are coprime. However, note that we assumed a_mod_2 = 0, which holds for n>=1. But what if n=0? Not allowed.
Thus solution appears correct.
Now, write final answer.
But before finalizing, think about alternative methods: Could they expect simpler? Possibly they expected participants to use the identity a = C(2n,n) and then compute a^b mod (10^9+6) by computing C(2n,n) using dynamic programming or precomputed factorials mod (10^9+6) using modular inverses with extended Euclid? However, as argued, modulus is not prime, so standard factorial inverses won't work because denominator might not be invertible modulo modulus due to factor 2. But we could compute C(2n, n) modulo (10^9+6) by using the multiplicative formula and dividing by canceling factors of 2 as we go? For example, compute numerator product (2n)! and divide by (n!)^2 by iterating and using gcd to cancel common factors before taking modulus. This is possible: iterate i from 1 to 2n, maintain numerator and denominator in factorized form or use reduction by gcd. But that would be O(n) per test case, and t up to 1000, n up to 1e5 => O(1e8) might be borderline but possibly okay in C++ if optimized? However, we also need exponentiation. Precomputation O(2e5) is better.
Given the constraints, the CRT method is efficient.
Now, we need to be careful about reading input: first line t, then t lines each with m n.
Now, implement.
Now, test with sample:
Input:
4
1 2
1 1
2 1
2 3
Our algorithm:
Precompute factorial mod P up to 200k.
Case1: m=1,n=2 => aP = C(4,2)=6 mod P =>6; temp=6inv2=6250000002%P=3; a=6; ans=6^1=6.
Case2: m=1,n=1 => a=2; ans=2.
Case3: m=2,n=1 => a=2; ans=4.
Case4: m=2,n=3 => a=20; ans=400.
Matches.
Now, consider edge: n=100000,m=1. Compute aP = C(200000,100000) mod P. That can be computed quickly with precomputed factorials. Then a = 2*(aP*inv2%P). Then output a mod MOD. That's fine.
Now, about overflow in factorial precomputation: fact[200000] modulo P. Multiply step: fact[i] = fact[i-1] * i % P. Since i up to 200000, fact[i-1] < P, product < P * 200000 ~ 1e14. In C++ long long (signed) holds up to 9e18, so safe. However, some compilers might have long long as 64-bit, safe.
But note: when computing inv_fact[i-1] = inv_fact[i] * i % P, again product < P * 200000 ~ 1e14, safe.
When computing aP = fact[2n] * inv_fact[n] % P * inv_fact[n] % P, we might compute as (fact[2n] * inv_fact[n]) % P, then * inv_fact[n] % P. Each multiplication product < P^2 ~ 2.5e17, which still fits in signed 64-bit? 2.5e17 < 9.22e18, so safe. But if we do fact[2n] * inv_fact[n] could be up to (P-1)(P-1) ≈ 2.5e17, still fine. Then multiply by inv_fact[n] again could be up to (2.5e17)(P-1) ≈ 2.5e17 * 5e8 = 1.25e26, which exceeds 64-bit. However, we do modulo after each multiplication. So we should do:
ll aP = fact[2*n];
aP = aP * inv_fact[n] % P;
aP = aP * inv_fact[n] % P;
So never multiply two numbers both near P without modulo. The intermediate after first multiplication is reduced modulo P, so it's less than P. So second multiplication product < P * (P-1) ≈ 2.5e17, still safe. So okay.
Now, power_mod: base a may be up to ~1e9, square up to 1e18, safe. Res may also be up to ~1e9, multiply by base yields up to 1e18, safe.
Thus everything fits.
Now, final step: produce answer.
But wait: we must ensure that we use the correct modulus for exponentiation: MOD = 1000000006.
Now, is there any trick? The exponent b = m, which is up to 1e5, but the exponent could be large, but exponentiation is O(log m). No issues.
Now, think about scenario where a = 0 mod MOD? As argued, not possible, but if it were, 0^m = 0 for m>0, but it's not.
Now, let's double-check identity: a = sum_{k=0}^n (nCk)^2 = C(2n, n). That's correct.
Thus solution is solid.
Now, we need to produce code.
But before finalizing, maybe we should consider that the modulus for output is 10^9+6, but we used MOD = 1000000006. Good.
Now, we should test a few random small values by brute force to ensure our method yields the same as direct computation mod MOD (with small numbers where we can compute C(2n,n) exactly and then mod). Let's do mental test for n=6, m=1.
C(12,6)=924. mod 1000000006 = 924. Compute our method: aP = 924 mod P (P=500000003) = 924. temp = 924inv2 mod P. inv2 = 250000002. Compute 924/2 = 462, but mod? Since 924 is even, dividing by 2 gives 462, but mod P it's 462? Because 4622=924 ≡924 mod P, so temp = 462. Then a = 2*462 = 924. So correct.
Now, n=7, C(14,7)=3432. mod MOD =3432. aP=3432 mod P =3432 - 500000003*0 = 3432. 3432 even, temp=1716, a=3432. Works.
Now, n=8, C(16,8)=12870. mod MOD=12870. aP=12870 mod P =12870. Even, temp=6435, a=12870.
Now, n=9, C(18,9)=48620. mod MOD=48620. aP=48620 mod P =48620. Even, temp=24310, a=48620.
Now, n=10, C(20,10)=184756. mod MOD=184756. aP=184756, even? 184756 is even? 184756/2=92378, yes even. So temp=92378, a=184756.
Now, try n=11, C(22,11)=705432. Even, a=705432. Good.
Now, consider n=13? It's fine.
Now, what about n=100? We trust.
Thus seems consistent.
Now, consider if the modulus was 10^9+7, then a^b mod 10^9+7 would be computed similarly but with prime modulus. But they gave 10^9+6, so we proceed.
Now, I'll produce final answer.
But before that, I want to think about potential pitfalls:
-
In the CRT, we assumed a_mod_2 = 0. But is it always 0 for n>=1? Yes, because C(2n, n) is even. Proof: C(2n,n) = (2n)!/(n! n!). The exponent of 2 in C(2n,n) is number of carries when adding n+n in base 2, which is at least 1 for n>0. So it's even. So a_mod_2 = 0.
-
However, note that we are computing a_mod_M, which is a mod (2P). Since a is even, it's consistent.
-
But what if a is divisible by 2P? That would require a multiple of P, but as argued, impossible because P > 2n. So a mod 2P is not 0, but could be 2, 4, ... etc. So fine.
-
In the CRT solution, we derived a = 2 * (aP * inv2 mod P). But note that this formula assumes that a is even and that the solution to x ≡ 0 (mod 2) and x ≡ aP (mod P) is unique modulo 2P. However, we need to ensure that such an x exists (i.e., the congruences are compatible). Since a is even, a mod P = aP, so the compatibility condition is that aP is even? Actually, if there exists an integer x satisfying x ≡ 0 mod2 and x ≡ aP mod P, then x must be even and congruent to aP mod P. Write x = 2k. Then 2k ≡ aP (mod P). This requires that aP is congruent to 2k mod P, i.e., that aP is even? Not necessarily; k can be such that 2k ≡ aP (mod P) even if aP is odd, because mod P, 2k can be odd if P is odd. For example, aP =1, P=5, we have 2*3=6≡1 mod5, so k=3 works. So the congruence is solvable regardless of parity of aP because 2 is invertible mod P. So there is always a solution. So our formula works for any aP.
Thus it's fine.
Now, confirm the exponentiation: Since MOD is composite, but exponentiation is fine.
Now, code.
But wait: Could there be any issue with the exponent b being large and we need to compute a^b mod MOD where a and MOD are not coprime? That's okay; fast exponentiation works regardless.
Now, about precomputation limit: We need to precompute up to 200000. But if t is 0? t≥1.
Now, all good.
But we must also consider that the input might be large, so use fast IO.
Now, produce final code.
But before final, let's test with a random larger n and m manually (in mind) to ensure no overflow.
Now, I'm thinking: The step a = 2 * temp; But what if temp = (aP * inv2 % P) is computed as aP * inv2 % P. Since inv2 = (P+1)/2, we could also compute a = (2 * aP * inv2) % MOD. But that is same as (aP * (2inv2 % MOD))? Actually 2inv2 = 2 * (P+1)/2 = P+1 ≡ 1 mod P, but not mod MOD. So not simpler.
Now, potential performance: t up to 1000, each pow_mod O(log m) ~ 17 iterations, fine.
Now, final answer.
But let's also note: Could we compute a directly as C(2n,n) mod MOD by using the formula C(2n,n) = product_{i=1}^{n} ( (n+i)/i ) and multiply stepwise, taking modulo MOD, and using modular division only when denominator is coprime to MOD. Since MOD has factor 2, many denominators i may be even, causing non-coprime. But we could cancel the factor 2 in numerator and denominator systematically. However, CRT method is simpler.
Now, I'll produce the solution in C++.
But I need to be absolutely sure about the primality of 500000003. Let's do a more rigorous check: I know that 500000003 is a prime number from memory. I recall that 500000003 is indeed a prime. For example, in Codeforces Round #716 (Div.2) problem B, they used modulus 1000000006? Not sure. However, I recall that 500000003 is a prime because 500000003 = (10^9+6)/2 and 10^9+6 is 2 * prime. Actually, 10^9+7 is prime; 10^9+6 = (10^9+7) -1, which is even, so its only prime factors are 2 and another number. That other number is (10^9+6)/2 = 500000003. If 500000003 were composite, then 10^9+6 would have more prime factors. Is it known? I think 500000003 is prime. I can quickly verify by checking divisibility by primes up to 10000 maybe, but not necessary. But to be thorough, I can test small primes up to 100: I did up to 31, all non-zero. Continue up to 100: 37,41,43,47,53,59,61,67,71,73,79,83,89,97. For each, compute 500000003 mod p. Let's quickly compute mod for some:
We already did mod 37 = 22. mod 41: earlier I didn't compute; let's compute: 10^8 mod 41 maybe simpler: 4112195122 =? But we can compute 500000003 mod 41. 4112195122 = 4112,000,000=492,000,000; 41195,122= 8,000,002? Actually 195,12240=7,804,880; +195,122=8,000,002; sum=500,000,002? 492,000,000+8,000,002=500,000,002. Remainder 1. So 500000003 mod 41 = 2? Wait: 4112195122 =? Let's compute properly: 41 * 12,195,122 = 12,195,122*40 = 487,804,880; plus 12,195,122 = 500,000,002. So remainder = 1. Actually 500,000,003 - 500,000,002 = 1, so remainder 1. So not divisible.
mod 43: 4311627907 = 4311,627,907? 4311,627,907 = 11,627,90740 = 465,116,280; +*3 = 34,883,721; sum = 500,000,001? Actually 465,116,280+34,883,721=500,000,001. Remainder 2. So not.
mod 47: 4710638298 = 4710,638,298 = 10,638,29840=425,531,920; 7=74,468,086; sum = 500,000,006? That's >500M. Let's compute: 4710,638,298 =? Actually 10,638,29850 = 531,914,900, too high. Let's do 500,000,003/47 = 10,638,297.936... So 4710,638,297 = 4710,000,000=470,000,000; +47638,297= 638,29747 = 638,297*40=25,531,880; +*7=4,468,079; sum=25,531,880+4,468,079=30,000 -? Actually 25,531,880+4,468,079=29,999,959; add to 470,000,000 = 499,999,959. Remainder = 44. So not.
mod 53: 539433962 = 539,433,962 = 9,433,962*50=471,698,100; *3=28,301,886; sum=500,000 -? 471,698,100+28,301,886=500,000 -? That's 500,000 -? Actually 471,698,100+28,301,886=500,000, - I think it's 500,000, - let's add: 471,698,100+28,301,900=500,000,000, subtract 14 = 499,999,986? Wait, 28,301,886 is 14 less than 28,301,900, so sum = 500,000,000 -14 = 499,999,986. So remainder = 500,000,003-499,999,986 = 17. So not.
mod 59: 598474576 = 598,474,576 = 8,474,57660=508,474,560 minus 8,474,576 = 500,000 -? Actually 8,474,57659 = 8,474,576*50=423,728,800; *9=76,271,184; sum=500,000 -? 423,728,800+76,271,184=500,000 -? That's 500,000 -? 423,728,800+76,271,200=500,000,000, minus 16 = 499,999,984. So remainder = 19. Not.
mod 61: 618196721 =? 618,196,721 = 8,196,721*60=491,803,260 +8,196,721 = 500,000 -? 491,803,260+8,196,740=500,000,000; minus 19 = 499,999,981. So remainder = 22.
mod 67: 677462687 = 677,462,687 = 7,462,68760=447,761,220; 7=52,238,809; sum=500,000,029? 447,761,220+52,238,809=500,000,029. That's 26 more than 500,000,003? Actually 500,000,029 > 500,000,003, so maybe 7,462,686? Let's compute 677,462,686 = 677,462,687 -67 = 500,000,029-67=499,999,962. Remainder = 41. So not.
mod 71: 717042254 = 717,042,254 = 7,042,25470=492,957,780; +7,042,254 = 500,000,034. Slightly over. So 7,042,25371 = 500,000,034-71=499,999,963. Remainder = 40.
mod 73: 736849315 =? 736,849,315 = 6,849,315*70=479,452,050; +*3=20,547,945; sum=500,000 -? 479,452,050+20,547,945=500,000 -? Actually sum=500,000 -? 479,452,050+20,547,950=500,000,000, minus 5 = 499,999,995. So remainder = 8.
mod 79: 796329114 = 796,329,114 = 6,329,11480=506,329,120 -6,329,114 = 500,000,006. So 6,329,11379 = 500,000,006-79=499,999,927. Remainder = 76.
mod 83: 836024096 = 836,024,096 = 6,024,096*80=481,927,680; +*3=18,072,288; sum=500,000 -? 481,927,680+18,072,288=500,000 -? Actually 481,927,680+18,072,320=500,000,000, minus 32 = 499,999,968. So remainder = 35.
mod 89: 895617978 = 895,617,978 = 5,617,97890=505,618,020 -5,617,978 = 500,000,042. So 5,617,97789 = 500,000,042-89=499,999,953. Remainder = 50.
mod 97: 975154639 = 975,154,639 = 5,154,639100=515,463,900 -5,154,6393=15,463,917; actually compute: 5,154,63997 = 5,154,639(100-3) = 515,463,900 - 15,463,917 = 500,000 -? 515,463,900-15,463,917=499,999,983. Remainder = 20.
So not divisible by any prime up to 97. That's promising. Could have factor 101? But we can trust it's prime. I'm comfortable.
Now, implement.
One more nuance: The modulus for output is 10^9+6. In our code we used MOD = 1000000006. But we also have P = 500000003. The product 2*P = 1000000006 exactly equals MOD. So MOD = 2 * P. That means we could compute a_mod_M directly as described.
But note: In the CRT combination we computed a = 2 * (aP * inv2 % P). Since inv2 = (P+1)/2, we can also compute a = (2 * aP * inv2) % MOD, but easier as above.
Now, test with n=0? Not needed.
Now, edge: What if m is large, like 100000, and we compute a^m mod MOD using fast exponent. No issues.
Now, maybe there is a trick: Since a = C(2n, n), and n up to 100k, we can precompute a values for all n up to 100k mod MOD using recurrence? But our method is fine.
Now, I'll write final solution.
Potential integer type: Use long long.
Implementation plan:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 200000; // 2 * 100000
const ll MOD = 1000000006LL;
const ll P = 500000003LL;
const ll INV2 = (P + 1) / 2; // 250000002
vector<ll> fact(MAX+1), inv_fact(MAX+1);
ll mod_pow(ll a, ll b, ll mod) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
void precompute() {
fact[0] = 1;
for (int i = 1; i <= MAX; i++) {
fact[i] = fact[i-1] * i % P;
}
inv_fact[MAX] = mod_pow(fact[MAX], P-2, P);
for (int i = MAX; i >= 1; i--) {
inv_fact[i-1] = inv_fact[i] * i % P;
}
}
ll central_binom_mod(ll n) {
// return C(2n, n) % MOD
ll aP = fact[2*n] * inv_fact[n] % P * inv_fact[n] % P;
ll temp = aP * INV2 % P;
ll a = 2 * temp; // less than MOD
return a;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
precompute();
int t;
cin >> t;
while (t--) {
ll m, n;
cin >> m >> n;
ll a = central_binom_mod(n);
ll ans = mod_pow(a, m, MOD);
cout << ans << "\n";
}
return 0;
}
But wait: The function central_binom_mod uses fact[2n] and inv_fact[n]. But we need to ensure 2n <= MAX. Since n <= 1e5, 2*n <= 2e5 = MAX, fine.
Now, check with sample.
Now, consider possibility that m=0? Not in constraints, but if it were, mod_pow would return 1 for a^0=1, but constraints m>=1.
Now, done.
But there is still a subtle issue: The modulus for final answer is 10^9+6, but we computed a as 2temp, which is < MOD, but is it always the correct C(2n,n) mod MOD? Let's verify with a small test where MOD is composite: M=10, P=5, n=2 gives a=6, our method yields 6 mod10, correct. n=4: C(8,4)=70 mod10 =0? Actually 70 mod10 =0. Our method: aP=70 mod5=0, temp=0, a=0, correct. n=5: C(10,5)=252 mod10 =2. aP=252 mod5=2, temp=2inv2 mod5 =2*3=6 mod5=1, a=2, correct. So works.
Now, what about a scenario where the actual a is negative? No.
Thus solution is correct.
Now, I should note: The problem may require using 10^9+6, but I've used 1000000006. Good.
Now, final answer: Provide the code.
But before final, we need to check if there's any case where 2n > MAX? Constraint n up to 1e5, so max 2n = 200k, we set MAX = 200000. Should be okay. But to be safe, we can set MAX = 200000, or better compute max_n from input.
Now, final.
But wait: There's also possibility that t is large and we need to precompute factorials only up to max 2n encountered. Could read all test cases first to find max_n, then precompute up to 2max_n. But given max n is 1e5, we can precompute up to 200000 unconditionally. It's fine.
Now, memory: two arrays of size 200001 of long long -> 20000182 ≈ 3.2 MB, fine.
Now, finalize.
But I'd like to double-check that the mod_pow for exponentiation uses MOD = 1000000006. In the function, we compute a * a % MOD. Since a < MOD, a*a can be up to (1e9)^2 = 1e18, fits in long long (9e18). So safe. However, if we used int, it would overflow. So use long long.
Now, edge: a could be 0? unlikely, but if a=0, then a^m = 0 for m>0, mod_pow would compute 0 correctly. But a cannot be 0 as argued. But still safe.
Now, I think solution is ready.
Now, we produce final answer.
But before delivering, I'll run through a couple more test cases manually.
Test: t=1, m=3, n=2. a = C(4,2)=6. ans = 6^3=216 mod 1000000006 =216. Our code: aP=6, temp=3, a=6, pow(6,3)=216. Good.
Test: m=100000, n=1. a=2. ans = 2^100000 mod 1000000006. That's some number. We'll trust.
Now, ensure that precomputation of inv_fact[MAX] using mod_pow with exponent P-2 works. Since P is prime, that's correct.
But wait: For P = 500000003, P-2 = 500000001. That's fine.
Now, check that fact[MAX] is not 0 mod P. Since P > MAX, fact[MAX] is product of numbers 1..MAX, all less than P, and P is prime, so none of these numbers are multiples of P. So fact[MAX] is not divisible by P, thus invertible. Good.
Now, all set.
Now, I'll produce final answer.
But there is an alternative: Could we avoid CRT by directly computing a = C(2n, n) mod MOD using the multiplicative formula and dividing by 2 appropriately? Possibly, but CRT is fine.
Now, I'll output final code.
One more nuance: In C++ when we compute 2 * temp, temp is long long, product within range.
Now, final answer.
But wait: The problem statement: "print modulo 10^9+6". They might expect answer as integer modulo that. Yes.
Now, I'm satisfied.
Thus final solution.
Now, I will produce a final answer with explanation and code. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 200000; // 2 * maximum n
const ll MOD = 1000000006LL; // required modulus
const ll P = 500000003LL; // prime factor of MOD
const ll INV2 = (P + 1) / 2; // modular inverse of 2 modulo P (250000002)
vector fact(MAX + 1), inv_fact(MAX + 1);
// fast exponentiation (a^b) % mod
ll mod_pow(ll a, ll b, ll mod) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
// precompute factorials and inverse factorials modulo P
void precompute() {
fact[0] = 1;
for (int i = 1; i <= MAX; ++i)
fact[i] = fact[i - 1] * i % P;
inv_fact[MAX] = mod_pow(fact[MAX], P - 2, P); // Fermat, P is prime
for (int i = MAX; i >= 1; --i)
inv_fact[i - 1] = inv_fact[i] * i % P;
}
// returns C(2n, n) modulo MOD ( = 2P )
ll central_binom_mod(ll n) {
ll aP = fact[2 * n] * inv_fact[n] % P * inv_fact[n] % P; // modulo P
ll temp = aP * INV2 % P; // (aP / 2) mod P
ll a = 2 * temp; // now a ≡ 0 (mod 2) and a ≡ aP (mod P)
// 2temp < 2*P = MOD, so no extra modulo needed
return a;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
precompute();
int t;
cin >> t;
while (t--) {
ll m, n;
cin >> m >> n;
ll a = central_binom_mod(n);
ll ans = mod_pow(a, m, MOD);
cout << ans << '\n';
}
return 0;
}