Joker is back again with his destructive plan . He has set N bombs in various parts of Gotham city and has challenged the police department to defuse all bombs in one day . City can be treated as a 1-D line with 'N' bombs situated in such a manner that distance of 1^st bomb is 'x' , 2nd bomb is ' x^2 ' , 3rd bomb is ' x^3 ' ....... nth bomb is ' x^n ' , where ' x ' is measured from the start (left end ) . Since the number of bombs planted is very large , Gordon and his team find it impossible to diffuse all the bombs in one day and once again Gordon asks for Batman's help . He keeps his bomb diffusing squad ready at the start and requests batman to bring all the bombs back to the start . Since the bombs are very heavy , batman can carry only one bomb at a time . For each bomb , he first travels upto the bomb location and then carry it back upto the start and thus covering the same distance ( bomb's distance from the start ) twice . For example for N=4 , total distance covered by batman is
x + x + x^2 + x^2 + x^3 + x^3 + x^4 + x^4 where x is the distance of the first bomb in kms.
Now Batman is wondering how much distance he has to cover in order to bring all bombs back to the start but he is little weak in calculations involving large numbers and asks for your help to calculate the total distance he has to cover . Since the total distance can be very large , he wants you to calculate the total distance mod M , i.e he wants total_distance % M.
Input
First line of input contains integer t , number of test cases. Each t lines contains contains 3 integers N - number of bombs planted in the city , x - distance of first bomb and M - modulo .
Output
Each line of output contains a single integer , the required answer.
Constraints
1 ≤ T ≤ 50
1 ≤ N,x ≤ 10^9
1< M ≤ 100000 (10^5)
We need to compute total distance: for each bomb at distance x^i (i from 1 to N), Batman travels from start to bomb and back, so distance = 2 * x^i. Thus total distance = 2 * (x + x^2 + ... + x^N) = 2 * (x * (x^N - 1) / (x - 1)) if x != 1. If x = 1, each bomb distance = 1^i = 1, so total distance = 2 * N * 1 = 2N. But careful: when x=1, the distances are all 1 (since 1^i = 1), so sum = N, times 2 = 2N.
We need total_distance mod M. Constraints: N, x up to 1e9, M up to 1e5 (only 100000). T up to 50.
We need to compute 2 * (x * (x^N - 1) / (x - 1)) mod M. But division mod M is not straightforward because M is not necessarily prime, and denominator may not be invertible mod M. However M is up to 1e5, small. N up to 1e9. So we need to compute the sum S = x + x^2 + ... + x^N mod something (but careful with division by (x-1) when x mod something equals 1). However we can compute sum modulo M directly using exponentiation and geometric series, but the denominator may cause trouble. Since M is small, we could compute sum iteratively? N up to 1e9, cannot iterate. But we can use periodicity modulo M, as exponentiation mod M and sum mod M may be periodic due to modulus small. However the sum of geometric progression modulo M with division might be computed using formula with modular inverse only if denominator invertible modulo M. If not invertible, we can't directly use that.
Alternative: Since M is small (≤1e5), and N and x up to 1e9, we could compute the sequence of residues x mod M, x^2 mod M, etc. But the series is geometric, we can compute the sum of geometric series using exponentiation by squaring that works even when denominator not invertible: we can compute (x^n - 1) / (x - 1) modulo M by using binary exponentiation with matrix or recursion, by computing (A^n - I) * (A - I)^{-1}? Hmm.
We can treat sum of geometric series: S = 1 + x + x^2 + ... + x^{N-1} perhaps? Our sum is from i=1 to N: x + ... + x^N = x*(1 + x + ... + x^{N-1}) = x * (x^N - 1)/(x - 1). So we need to compute (x^N - 1)/(x - 1) modulo M, multiplied by x and 2.
Instead of division, we can compute the sum using recursion:
Define pow(x, n) mod M. For sum geometric: Let f(n) = 1 + x + x^2 + ... + x^{n-1} mod M. Then f(2n) = f(n) * (1 + x^n) mod M? Actually f(2n) = (1 + x + ... + x^{n-1}) + x^n (1 + x + ... + x^{n-1}) = f(n) * (1 + x^n) mod M. Also f(n+1) = f(n) + x^n. Using binary exponentiation we can compute f(n) in O(log n) time by leveraging exponentiation by squaring with a pair (current power, current sum). This is standard: compute both x^n and sum_{i=0}^{n-1} x^i simultaneously using exponentiation by squaring.
Method: Use recursion or iterative method: We want to compute (x^N mod M, S = sum_{i=0}^{N-1} x^i mod M). Then our desired sum from i=1 to N is x * S mod M? Actually S = sum_{i=0}^{N-1} x^i = (1 - x^N)/(1-x). Our sum from i=1 to N is x * (1 - x^N)/(1-x) maybe? Let's define:
We want total = 2 * (x + x^2 + ... + x^N) = 2 * (x * (1 + x + ... + x^{N-1})).
So if we compute S = sum_{i=0}^{N-1} x^i mod M, then result = 2 * x * S mod M. However careful: For N=0? N≥1, fine.
So we need S modulo M, where M ≤ 1e5. Since M is not large, we can compute exponentiation mod M and the sum mod M using the binary exponentiation method that handles sums.
Standard approach: Define a function that given x, n, M returns (pow, sum) where pow = x^n mod M, sum = (1 + x + ... + x^{n-1}) mod M.
We can compute recursively:
If n == 0: return (1, 0) (since x^0 = 1, sum_{i=0}^{-1}? Actually for n=0, sum of 0 terms is 0)
Else if n is even: compute (p, s) for n/2. Then x^{n} = p * p mod M. And sum for n: s_total = s + p * s mod M? Let's derive:
We have two halves: For n = 2k, sum_{i=0}^{2k-1} x^i = (1 + x + ... + x^{k-1}) + x^k (1 + x + ... + x^{k-1}) = s(k) + x^k * s(k) = s(k) * (1 + x^k). But careful: x^k is p (which is x^k mod M). So s_total = s * (1 + p) mod M. And x^{2k} = p * p mod M.
If n is odd: n = 2k+1. Then sum_{i=0}^{2k} x^i = sum_{i=0}^{2k-1} x^i + x^{2k}. But we can compute using even part: We can compute for 2k as above and then add x^{2k} term. Alternatively use recursion: compute for k, then extend. Common approach:
function power_sum(x, n, M):
if n == 0: return (1, 0) # x^0, sum_0
(p, s) = power_sum(x, n//2, M)
p2 = p * p % M
if n % 2 == 0:
# n = 2k
# sum_{i=0}^{2k-1} = s*(1 + p) (since p = x^k)
s2 = s * (1 + p) % M
return (p2, s2)
else:
# n = 2k+1
# first compute for 2k: sum_{i=0}^{2k-1} = s*(1+p) (here s corresponds to k, p = x^k)
# then we need sum_{i=0}^{2k} = sum_{i=0}^{2k-1} + x^{2k}
# x^{2k} = p2 = p*p
# but careful: For odd n, we need sum of n terms: i=0..(2k) inclusive? Actually our function expects sum for n terms: sum_{i=0}^{n-1} x^i. So for n = 2k+1, we need sum of first (2k+1) terms? Wait: n is the number of terms? In our definition, we pass n as the exponent count? Let's be precise:
We want for given N (where N is number of terms in the sum from 0 to N-1), we need sum S = sum_{i=0}^{N-1} x^i. So input n = N. In recursion, we compute for that n. So base: n=0 => sum=0. For n even: n=2k => sum_{i=0}^{2k-1} = (sum_{i=0}^{k-1} x^i) + x^k (sum_{i=0}^{k-1} x^i) = s(k) * (1 + x^k). So if we have (p, s) for n/2 = k, where p = x^k, s = sum_{i=0}^{k-1} x^i, then for n we compute sum = s * (1 + p) % M. For n odd: n=2k+1 => sum_{i=0}^{2k} = sum_{i=0}^{2k-1} + x^{2k}. We can compute (p, s) for k? Let's see: For n=2k+1, we could compute from the even case for 2k, then add x^{2k}. But we can also compute directly from (p, s) for floor(n/2) = k. But careful: floor(n/2) = k for odd n? Actually n//2 = k if n = 2k+1. So we have (p, s) for k, with p = x^k, s = sum_{i=0}^{k-1} x^i. Then we can compute:
x^{2k} = (x^k)^2 = p^2.
x^{2k+1} = x^{2k} * x.
But we need sum for n = 2k+1: sum_{i=0}^{2k} x^i. This can be expressed as: sum_{i=0}^{k-1} x^i + x^k * sum_{i=0}^{k-1} x^i + x^{2k}? Wait: Let's partition: For indices 0..2k, we can group as first k terms (0..k-1), then next k terms (k..2k-1), then the last term index 2k. But note that x^{k}..x^{2k-1} = x^k * (1 + x + ... + x^{k-1}) = p * s? Actually s = sum_{i=0}^{k-1} x^i. So sum_{i=k}^{2k-1} = x^k * s = p * s. Then plus the term x^{2k}. So total sum = s + ps + x^{2k} = s(1+p) + p^2. However is that correct? Check: For k=1, n=3: sum_{i=0}^{2} = 1 + x + x^2. s for k=1? s = sum_{i=0}^{0} = 1? Actually careful: For k=1, s = sum_{i=0}^{0} = 1, p = x^1 = x. Then s*(1+p) = 1*(1+x)=1+x. Then p^2 = x^2. Sum=1+x+x^2, correct. So indeed for odd n=2k+1, sum = s*(1+p) + p^2.
But is this consistent with the recursion pattern? We can compute p2 = pp mod M. Then for even we returned (p2, s(1+p)). For odd we need to return (p2 * x mod M? Because x^n for n=2k+1 is x^{2k+1} = p2 * x. So pow = p2 * x % M. And sum = s*(1+p) + p2. However note: For odd case, we should compute sum for n = 2k+1, i.e., sum_{i=0}^{2k} x^i. Yes.
But there is also alternative recursion: Compute for (n-1) then add x^{n-1}. But that would be O(n). So we stick with O(log n) method.
Thus algorithm: For each test case, compute (p, s) = power_sum(x, N, M) where s = sum_{i=0}^{N-1} x^i mod M. Then total = (2 * x % M * s) % M. For x=0? But constraints: x≥1, so fine.
But need to consider special case: x mod M may be 0? Actually x up to 1e9, M up to 1e5, so it's possible that x is multiple of M, then x mod M = 0. Then x^1 mod M = 0, x^2=0,... So sum = 0 (if N>=1?) Actually x mod M=0: then x^i mod M = 0 for i>=1. But sum_{i=0}^{N-1} x^i: first term i=0: x^0=1. So s = 1 mod M. Because 1 + 0 + 0 + ... = 1. Then total = 2xs mod M: x mod M=0, so 0. That seems correct: Batman travels to bomb at distance 0? Wait if x mod M=0 but actual distance is x, not mod M. The distance is actual integer x^i? The problem states distance of first bomb is x, second is x^2, etc. They are large numbers, but we compute total distance mod M. So if x is multiple of M, then x mod M =0, but actual distance is huge. But we only care about mod M. Indeed, if x is multiple of M, then each x^i is multiple of M for i>=1, so their values mod M are 0. So total distance = 2*(x + x^2+...+x^N) is divisible by M? Actually each term x^i is multiple of M, so sum is multiple of M, times 2 also multiple of M, so modulo M result is 0. However we also include s = sum_{i=0}^{N-1} x^i mod M? Wait careful: Our derived total = 2 * (x + x^2 + ... + x^N) = 2 * x * (1 + x + ... + x^{N-1}). In modulo M, if x mod M = 0, then x * something mod M = 0, so total = 0 mod M. That matches. But our computation s = sum_{i=0}^{N-1} x^i mod M: includes term i=0 = 1, but multiplied by x mod M = 0 gives 0. So it's fine.
Now the power_sum recursion needs to handle modulus M, but note that when M=1? Constraint says 1< M ≤ 100000, so M>=2, so okay.
But we have to be careful about overflow: intermediate multiplication may exceed 64-bit? p and s are mod M, so they are less than M ≤ 1e5, so pp up to 1e10, fits in 64-bit (since 1e10 < 2^63). p2 = pp % M, we can compute using 64-bit. In C++ we can use long long. In Python it's fine.
Now we need to compute power_sum efficiently. However, there is a nuance: The recursion described works for computing sum_{i=0}^{n-1} x^i mod M. But it assumes that we have (x mod M) and the operations are modulo M. That is correct.
But we must be careful: The formula s*(1+p) etc. is valid under modulo arithmetic. However, is the recursion valid when M is not prime? Yes, because we are just performing addition and multiplication mod M. The derivation does not require division.
Thus algorithm: For each test case:
Read N, x, M.
Compute x_mod = x % M.
If x_mod == 0:
Then total = 0? Actually we need to compute total = 2 * (x + x^2 + ... + x^N) mod M. Since each x^i for i>=1 is multiple of M? Not exactly: if x_mod == 0, then x is divisible by M, so x^2, x^3,... are divisible by M, but x itself may not be divisible by M if x < M? Wait x_mod == 0 means x % M == 0, so x is multiple of M. So x itself is divisible by M, so all x^i divisible by M for i>=1. So sum = multiple of M. Times 2 also multiple of M. So total mod M = 0. So answer is 0. That's simple and avoid recursion. However we need to consider if N=0? Not possible, N≥1.
But there is also case x_mod == 1? Then sum becomes something like N mod M? Actually if x mod M == 1, then x^i mod M = 1 for all i. Then s = sum_{i=0}^{N-1} 1 = N mod M. Then total = 2 * x * s mod M = 2 * 1 * (N mod M) mod M = 2N mod M. However also need to consider that x may be > M but x_mod == 1, then actual distances are huge, but modulo M it's 1. So that works.
But we must be careful: The recursion with power_sum works even when x_mod = 1 or 0, but we may also handle them separately to avoid any issues like (1+p) with p = 1 leads to s*(2) etc. It's fine.
Nevertheless, we should implement power_sum properly for all x_mod, N, M.
But note that N can be up to 1e9, recursion depth O(log N) ~ 30, fine.
Thus we can implement a function that returns (pow, sum) given (x_mod, n, M). However careful: For x_mod = 0, the recursion may still work? Let's test: x_mod = 0, n = N. Compute recursively:
Base n=0: (1,0)
For n>0, we compute (p,s) for n//2. For n=1 (odd): floor(1/2)=0 => (p,s) = (1,0). Then p2 = 11 % M = 1. Since odd, pow = p2 * x_mod % M = 10=0. sum = s*(1+p) + p2 = 0*(1+1) + 1 = 1. So for n=1, we get pow=0, sum=1, which matches: x^1 mod M = 0, sum_{i=0}^{0} x^i = 1? Actually sum for n=1 means sum of i=0 to 0 (1 term): 1. Yes. For n=2 (even): floor(2/2)=1 => compute for n=1 gave pow=0, sum=1. Then p = pow from that =0, s=1. p2 = pp=0. Since even: pow = p2 =0, sum = s(1+p)=1*(1+0)=1. For n=2, sum_{i=0}^{1} = 1 + x = 1 + 0 = 1, correct. So recursion works.
But we must ensure that when x_mod=0, we handle large N correctly. However, note that x_mod=0 leads to pow quickly becomes 0 for n>=1, and sum remains 1 for any N? Actually for n>=1, sum_{i=0}^{N-1} x^i mod M = 1 + 0 + 0 + ... = 1 mod M. So s = 1. The recursion yields s = 1 for all N>=1. Good.
Now we need to compute total = 2 * x_mod * s % M. However, careful: total = 2 * (x + x^2 + ... + x^N) mod M. We expressed as 2 * x * s, where s = sum_{i=0}^{N-1} x^i mod M. But is this correct modulo M? Let's double-check: total = 2 * (x + x^2 + ... + x^N) = 2 * x * (1 + x + ... + x^{N-1}) = 2 * x * s. However s here is sum of powers from 0 to N-1 of x. But when we compute s mod M, we compute sum modulo M. Then multiply by 2 and by x mod M, that yields 2xs mod M. But note that s is sum modulo M, and the product 2xs modulo M is equivalent to (2 * (x mod M) * (s mod M)) mod M. However, is it correct to reduce s modulo M before multiplication? Because total mod M = (2xs) mod M. Since mod multiplication is valid, we can compute s mod M first. So it's correct.
But one nuance: The formula total = 2xs is an equality over integers. However, x and s are integers; s may be huge. But we compute s mod M, but then we multiply by x mod M. However, it's true that (2xs) mod M = (2 * (x mod M) * (s mod M)) mod M, because (a mod M)(b mod M) mod M = (ab) mod M. So yes.
Thus algorithm: compute x_mod = x % M; if x_mod == 0: answer = 0 (since product includes x_mod). Actually if x_mod == 0, answer is 0 regardless of s (unless N=0 but not). So we can return 0 quickly.
But careful: Could there be case where x_mod != 0 but x mod M * s mod M = 0? That's fine, answer computed accordingly.
Now we need to implement power_sum(x_mod, N, M) to get s = sum_{i=0}^{N-1} x^i mod M. However note that N can be as high as 1e9, and we need to compute s for given N. The recursion above computes s for n = N. But is the recursion correct for all N? Let's test with small values.
Define function f(n) returns (pow = x^n mod M, sum = sum_{i=0}^{n-1} x^i mod M). We'll implement:
def power_sum(x, n, M):
if n == 0:
return (1 % M, 0)
(p, s) = power_sum(x, n // 2, M)
p2 = (p * p) % M
if n % 2 == 0:
# n even
sum_total = (s * (1 + p)) % M
return (p2, sum_total)
else:
# n odd
# first compute for 2k part: but we have p = x^k, s = sum_{i=0}^{k-1}
# For odd n = 2k+1: sum = s*(1+p) + p2
sum_total = (s * (1 + p) + p2) % M
pow_total = (p2 * x) % M
return (pow_total, sum_total)
Check: For n=1, half = 0 => (p,s) = (1,0). p2 = 1. n odd: sum = 0*(1+1)+1 =1, pow = 1 * x = x. correct: pow = x^1, sum = sum_{i=0}^{0}=1.
For n=2: half = 1 => (p,s) = (x,1). p2 = x^2 mod M. n even: sum = 1*(1+x) = 1 + x, pow = x^2. correct.
For n=3: half = 1 => (p,s) = (x,1). p2 = x^2. n odd: sum = 1*(1+x) + x^2 = 1 + x + x^2, pow = x^2 * x = x^3. correct.
For n=4: half = 2 => (p,s) from n=2: p = x^2, s = 1 + x. p2 = x^4. n even: sum = (1+x)*(1 + x^2) = 1 + x + x^2 + x^3. correct.
So recursion works.
Edge cases: M may be 1? But M > 1, so fine.
Now we need to implement for T up to 50, each O(log N). That's trivial.
But note: Input constraints: N up to 1e9, x up to 1e9, M up to 1e5. So x_mod = x % M is computed. But careful: In recursion, x is the base modulo M, so we should pass x_mod.
Potential pitfalls: For x_mod = 0 or 1, recursion works, but for x_mod = 1, p = 1 for all powers, so p2 = 11=1, and sums become like s(2) etc. That's fine.
But we must check for overflow in languages like C++. We'll use long long.
Now compute answer = (2 * ((x_mod * s) % M)) % M. Or ans = (2 * x_mod % M * s) % M.
But we should also test x_mod=0 case: e.g., N=3, x=5, M=5 => x_mod=0, answer should be 0. Compute: s = sum_{i=0}^{2} 0^i? Actually x_mod=0, but i=0: 1, i=1:0, i=2:0 => s=1 mod5=1. total=201=0. correct.
Edge: x_mod=0 and N=0? Not possible.
Now ensure recursion handles large N efficiently: O(log N) depth, fine.
Potential issue: When M=1? But M>1, so fine.
Now implement.
But note: In some languages, recursion may cause overhead; we can implement iteratively. But recursion depth is only ~30, fine.
But we need to be careful: The recursion uses many function calls per test case; with T=50, total calls ~1500, trivial.
Now about large N: In recursion we compute n//2, which is integer division. Works.
But is there any nuance regarding negative mod? Not needed.
Thus solution: For each test case:
Read N, x, M.
If M == 1: answer = 0 always, because mod 1 of anything is 0. But M>1 per constraints, but could be 1? Input says 1< M ≤ 100000, so M>=2, so no need to handle.
Compute x_mod = x % M.
If x_mod == 0: print 0.
Else:
Call function power_sum(x_mod, N, M) to get (p, s). But we only need s.
Compute ans = (2 * ((x_mod * s) % M)) % M.
Print ans.
But wait: Check for x_mod == 1: power_sum may compute s = N mod M. That is okay.
But is power_sum handling large N efficiently? Yes.
Now, confirm the power_sum definition: We pass n = N, because we want sum_{i=0}^{N-1} x^i. However, there is a nuance: For N=1e9, sum_{i=0}^{N-1} has N terms, but recursion handles that.
But there is also case when N=0? Not possible.
Now I'd like to double-check the recursion for odd case. The expression sum_total = (s * (1 + p) + p2) % M. Could there be any off-by-one? Let's test with n=1 we already did. With n=5: Let's compute manually. x=some value, mod M arbitrary but we can test with integers and see if recursion matches formula.
Let x=3, N=5, M large say 1000. Then s_desired = 1+3+9+27+81 = 121. Compute recursion:
n=5 odd: half = 2 (n//2=2). For n=2, compute:
n=2 even: half=1. For n=1 odd: half=0 -> (p,s) for 0 = (1,0). n=1: p=1, s=0? Actually from n=1 odd: we compute (p,s) from n=0: (1,0). Then p2=1, sum = 0*(1+1)+1=1, pow = 1*x = x=3. So (p,s) for n=1 = (3,1). Good.
Now for n=2 even: (p,s) from half=1 = (3,1). p2=9. n even: sum = s*(1+p) = 1*(1+3)=4, pow=9. So (p,s) for n=2 = (9,4). That sum is 1+3 =4, correct.
Now for n=5 odd: half =2 -> (p,s) from n=2 = (9,4). p2 = 81. n odd: sum = s*(1+p) + p2 = 4*(1+9) +81 = 410+81=40+81=121. pow = p2 * x = 813=243. So s=121 matches. Good.
So recursion correct.
Now also we might consider using exponentiation by squaring without recursion, but recursion is simpler.
Now, check potential issues with (1+p) when p = M-1? Then 1+p mod M = 0, that's fine.
Now, consider performance: Each recursion step does a few multiplications and additions. With N up to 1e9, recursion depth ~30, each step also includes a recursion call. So it's fast.
Now we also need to consider if M is not prime and x_mod - 1 may not be invertible, but we don't need division.
Thus solution stands.
But wait: Is the sum we computed s = sum_{i=0}^{N-1} x^i mod M? We multiply by x_mod to get sum from i=1 to N? Actually we multiply by x_mod to get sum from i=1 to N? Check: sum_{i=1}^{N} x^i = x * sum_{i=0}^{N-1} x^i = x * s. So total = 2xs. That's correct.
But we have to be careful: The distances are x, x^2, ... x^N. So indeed sum = x + x^2 + ... + x^N = x*(1 + x + ... + x^{N-1}).
Yes.
Now double-check sample: For N=2, x=8: s = 1+8=9, xs = 72, 272=144, mod100=44.
Thus algorithm.
Now we need to implement in code.
But there is another nuance: x_mod may be 0, we early returned 0. However, consider M=2, x=2, x_mod=0. Then answer should be 0. That matches.
But what about x_mod=0 but N=0? Not needed.
Now consider N extremely large, x_mod = something, but s computed by recursion. The recursion works for any n, as long as we handle n=0 correctly.
Now check for M=100000 (max) and x_mod large. The multiplications pp can go up to (M-1)^2 = about 1e10, which fits in 64-bit (since 10^10 < 9.22e18). In C++, unsigned long long or long long (signed) can hold up to 9.22e18, so safe. But we need to compute (s * (1+p)) mod M: s and p are less than M, so product up to (M-1)(M) about 1e10, safe. So using long long is okay.
Now implement.
But we might also need to handle the case where M=1? But constraints say >1, so skip.
Now I'll write the solution.
But wait: Could there be an issue with the recursion when n is large and we use recursion depth? Not.
Now test some random cases with brute force small values to verify.
Now test x_mod=1: N=1000000000, M=100000. s = N mod M = 1000000000 % 100000 = 0? 1e9 mod 1e5 = 0? Actually 1e9 / 100000 = 10000 exactly? 10000010000 = 1e9, yes remainder 0. So s=0, total=0. That seems plausible? If x=1, distances are all 1, total=2N. mod 100000, N=1e9 => 2*1e9 mod 100000 = 0. So answer 0.
Now test edge: N=1, x=10, M=3. x_mod=10%3=1. s=1 mod3=1. total=211=2 mod3=2. Compute manually: distance=10, 2*10=20 mod3=2. Yes.
Now test when x_mod = M-1 (i.e., -1 mod M). e.g., M=5, x_mod=4. Then s = sum_{i=0}^{N-1} (-1)^i mod5. That alternates. Our recursion should handle that.
Let's test N=4, x=4, M=5. x_mod=4. s = 1 + 4 + 16 + 64? Actually N=4 => sum i=0..3: 1 + 4 + 16 + 64 = 85 mod5 = 0 (since 85%5=0). total = 240=0 mod5=0. Compute manually: distances: 4,16,64,256? Wait N=4, so bombs at x, x^2, x^3, x^4? Actually our sum for bombs is x + x^2 + x^3 + x^4 = 4+16+64+256=340, 2*340=680 mod5=0. Yes.
Now test N=3, x=4, M=5: s = 1+4+16=21 mod5=1; total=241=8 mod5=3. Manual: bombs: 4+16+64=84, 2*84=168 mod5=3. Good.
Thus algorithm correct.
Now we should consider an alternative: Could we compute using geometric series formula with modular inverse if gcd(x-1, M) = 1? But that's not necessary and more complex.
Now about constraints: T up to 50, all okay.
Now implement function power_sum.
Potential bug: For large N, recursion may use many stack frames if implemented recursively. But depth ~30, okay. But we must ensure that the recursion termination condition is correct.
We could also implement iteratively using binary exponentiation: decompose N into bits, but the recursion approach is straightforward.
Let's finalize.
Now double-check the sample: Already matches.
Now final solution.
But wait: There is a nuance: The problem statement says "distance of 1st bomb is x, 2nd bomb is x^2, ..." etc. So distances are powers. Batman travels to bomb and back, covering distance twice. So total = 2*(x + x^2 + ... + x^N). Good.
Now but they gave sample: For N=4, total = x+x + x^2+x^2 + x^3+x^3 + x^4+x^4. Indeed.
Thus answer formula is correct.
Now confirm that we are not missing any factor like 2 maybe multiplied after modulo? Already accounted.
Now I'll produce final answer.
But before finalizing, consider if N=0? Not in constraints; 1 ≤ N. So ignore.
Now let's think about the possibility of overflow in the multiplication 2 * x_mod * s when all three are up to M-1 (1e5-1). product up to about 21e51e5 = 2e10, fits in 64-bit. But we will mod M after each multiplication to be safe.
Now code skeleton:
#include
using namespace std;
typedef long long ll;
pair<ll, ll> power_sum(ll x, ll n, ll M) {
if (n == 0) {
return {1 % M, 0};
}
auto [p, s] = power_sum(x, n/2, M);
ll p2 = (p * p) % M;
if (n % 2 == 0) {
ll sum = (s * (1 + p)) % M;
return {p2, sum};
} else {
ll sum = (s * (1 + p) + p2) % M;
ll pow = (p2 * x) % M;
return {pow, sum};
}
}
int main() {
int t; cin >> t;
while (t--) {
ll N, x, M; cin >> N >> x >> M;
ll x_mod = x % M;
if (x_mod == 0) {
cout << 0 << endl;
continue;
}
auto [p, s] = power_sum(x_mod, N, M); // s = sum_{i=0}^{N-1} x^i mod M
ll ans = (2 * ((x_mod * s) % M)) % M;
cout << ans << endl;
}
return 0;
}
Now test with sample.
Edge case: For x_mod = 0 we output 0, but we should also consider that when M=1? Not.
But wait: what if x_mod != 0 but M divides 2x_mods? That's fine.
Now, but is there a case where x_mod=0 but N=0? not.
Now, also consider that our power_sum returns p = x^N mod M. But we don't need it. That's fine.
Now, one more test: Large N and M=2, x=3. x_mod=1. Then s = N mod 2? Actually sum_{i=0}^{N-1} 1 = N mod 2. total = 2 * 1 * (N mod2) mod2 = 0 because 2 mod2=0, so answer always 0. Indeed, if M=2, 2something mod2 = 0. But is that correct? For x=3 (odd), distances are odd numbers? Actually 3,9,27,... all odd. Sum of odd numbers: if N is odd, sum odd? Actually odd+odd=even, etc. Then 2sum is even, mod2=0. So answer 0 always. That matches.
Now we should think about potential pitfalls: The recursion uses pair returns, but in C++ before C++17, we might need to use tie or declare separately. But we can use C++17, many judges support. Alternatively, we can implement iterative.
But we can also write iterative version.
Better to write in Python for simplicity, but the problem may be in any language. I'll produce answer in Python as it's easier for explanation. But the solution description can be language agnostic.
Given that sample input and output are small, but we need to produce code. In editorial, we can provide both explanation and pseudocode.
Now let's consider memory and time: fine.
Now we have solved.
But before finalizing, consider if there is any need to handle large N when x_mod = 1. In power_sum recursion, with x_mod=1, p = 1 always (since x^k mod M = 1). Then p2 = 1. For even case: sum = s * (1+1) = 2s mod M. For odd: sum = s*(1+1) + 1 = 2s+1 mod M. This recursion essentially doubles s each time, which might be O(log N) multiplications, but each step doubles the sum, which could cause large numbers? But we always mod M, so s remains within M. So it's fine.
Now the recursion may cause many operations, but still O(log N). However, we can also directly compute s = N mod M when x_mod=1. That could be a micro-optimization but not necessary. But early check for x_mod == 1: s = N % M. Then ans = (2 * (1 * (N%M))) % M = 2N % M. That might be slightly faster, but not required.
But we could include that for completeness.
Similarly, when x_mod == 0, we already handled.
Thus final answer includes these checks.
Now also consider M may be up to 100000, but x_mod may be 0,1.
Now we can finalize.
But we must be careful about a subtle point: The distances are x^i, where x is distance of first bomb. That is exactly x^1, x^2,... So if x=0? But constraint x≥1, so x>0. Good.
Now check: If x=1, then distance of bombs are all 1, so total distance = 2N. That's consistent.
Now we produce final answer.
Now note: The problem may have constraints that N and x can be up to 10^9, but M up to 10^5. So our solution is efficient.
Now provide final code.
Let's also verify with some random test to ensure recursion works for all N.
We can write a small test in mind: N=10^9, x=123456, M=99999. Compute using algorithm.
Now one more nuance: In recursion, we used (1 + p) mod M. But p is less than M, so 1+p may be >= M, we should do (1 + p) % M? Actually we use s * (1 + p) % M. Since multiplication and addition mod M, we can compute (1 + p) % M to keep numbers within range, but not necessary as long as we take mod after multiplication. However, (1+p) could be M, which mod M = 0. So better to compute (1+p) % M before multiplication to avoid overflow? But (1+p) up to M, which is at most 1e5, s up to 1e5, product up to 1e10, still safe. But to be safe, we can mod after multiplication. So it's fine.
But careful: For odd case, we compute s*(1+p) + p2. The sum of terms may exceed 64-bit if we don't mod intermediately? But s*(1+p) up to 1e10, plus p2 up to 1e10, sum up to 2e10, still within 2^63 (~9.22e18). So safe.
Now implement.
Now final code in C++:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
pair<ll,ll> power_sum(ll x, ll n, ll M) {
if (n == 0) return {1 % M, 0};
auto [p, s] = power_sum(x, n >> 1, M);
ll p2 = (p * p) % M;
if (n & 1) {
// odd
ll sum = (s * (1 + p) + p2) % M;
ll pow = (p2 * x) % M;
return {pow, sum};
} else {
// even
ll sum = (s * (1 + p)) % M;
return {p2, sum};
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while (t--) {
ll N, x, M; cin >> N >> x >> M;
ll xm = x % M;
if (xm == 0) {
cout << "0\n";
continue;
}
if (xm == 1) {
ll s = N % M; // because sum = N (mod M) as each term 1
ll ans = (2 * ((xm * s) % M)) % M; // xm=1
cout << ans << "\n";
continue;
}
auto [p, s] = power_sum(xm, N, M);
ll ans = (2 * ((xm * s) % M)) % M;
cout << ans << "\n";
}
return 0;
}
But we used xm==1 check, but then we compute ans = 2* (1 * s) % M = 2s % M. Since s = N%M, ans = 2*(N%M) % M. That's fine.
But we can also combine the xm==0 and xm==1 checks.
Now check if xm==1 case also works with power_sum? It would work as well, but this avoids recursion overhead.
Now test for xm==1, M=10, N=10 -> s=0, ans=0. Good.
Now, but wait: For xm==1, the sum of distances is 2N. However, if M is even, 2N mod M might be computed as 2*(N%M) mod M. That's correct.
Now test for xm==1, N=5, M=3: N%3=2, ans=4%3=1. Manual: total=2*5=10 mod3=1. Yes.
Thus good.
Now for xm==0 case, we output 0.
But what about xm == 0 and N=0? Not needed.
Now finalize.
But we should double-check the case where xm != 0 but maybe xm-1 not invertible, but we don't use inverse.
Now we produce final answer.
I'll write solution explanation accordingly.
Now I'd like to test for a tricky case: N=1, x=2, M=2. x_mod=0. Output 0. Manual: distance=2, total=4 mod2=0. Correct.
N=1, x=3, M=2. x_mod=1. s = 1 (since sum_{i=0}^{0}=1). ans = 211=2 mod2=0. Manual: distance=3, total=6 mod2=0. Yes.
Now another: N=2, x=3, M=2. x_mod=1. s = N%2? Actually x_mod=1 => s = sum_{i=0}^{1}1 = 2 mod2=0. ans=0. Manual: distances:3+9=12, 2*12=24 mod2=0. Yes.
Now test: N=2, x=3, M=4. x_mod=3. Compute s via recursion: n=2: need p,s for n=1: from n=0: (1,0) -> n=1: p=3, s=1. Then for n=2: p=9%4=1, s = s*(1+p)=1*(1+3)=4%4=0. So s=0. ans=230=0. Manual: distances:3+9=12, total=24 mod4=0. Yes.
Now test: N=3, x=3, M=4. n=3: from n=1: p=3, s=1? Wait compute power_sum for n=3: half=1 => (p=3,s=1) from n=1. p2=9%4=1. n odd: sum= s*(1+p)+p2 =1*(1+3)+1=4+1=5%4=1. So s=1. ans=231=6%4=2. Manual: distances: 3+9+27=39, total=78 mod4? 78%4=2 (since 4*19=76). Yes.
So all good.
Now note: The recursion uses a lot of modulo operations, but that's fine.
Thus solution is correct.
Now, ensure that we use proper data types. In Python, no issues.
Now, let's think about possibility of N being 0? If N=0, no bombs, total distance 0. But constraints say N≥1, so we can ignore. But if we want to be safe, we could handle.
Now final solution in Python for simplicity:
def power_sum(x, n, M):
if n == 0:
return (1 % M, 0)
p, s = power_sum(x, n // 2, M)
p2 = (p * p) % M
if n % 2 == 0:
return (p2, (s * (1 + p)) % M)
else:
return ((p2 * x) % M, (s * (1 + p) + p2) % M)
t = int(input())
for _ in range(t):
N, x, M = map(int, input().split())
xm = x % M
if xm == 0:
print(0)
continue
if xm == 1:
s = N % M
ans = (2 * ((xm * s) % M)) % M
print(ans)
continue
p, s = power_sum(xm, N, M)
ans = (2 * ((xm * s) % M)) % M
print(ans)
But note: When xm == 1, we compute ans = (2 * ((1 * s) % M)) % M = (2 * s) % M. That's fine.
But we can also just compute ans = (2 * (N % M)) % M, since xm=1. So we can simplify.
Now check sample: For 4th test case: N=2, x=8, M=100. xm=8, not 0 or 1, so compute power_sum(8,2,100): n=2: half=1 -> power_sum(8,1,100): from n=0 => (1,0); n=1: p=8, s=1; then n=2: p2=64, s=1*(1+8)=9, return (64,9). Then ans = 2 * ((89)%100) = 272=144%100=44. Good.
Now complexity: O(T log N).
Now final answer.
But we need to consider that M can be up to 100000, and x may be large, but we reduce modulo M. However, note that the recursion uses x_mod which is less than M. But when x_mod and M are not coprime, the recursion still works.
Now I think solution is correct.
Now we can produce final answer.
But wait, there's one more nuance: The problem says "distance of 1st bomb is x, 2nd bomb is x^2, ..., nth bomb is x^n". But x is measured from the start. So distances are x, x^2, ..., x^n. That's what we used. Good.
Now what about x^0? Not used.
Now we are done.
Now I'll craft final response with explanation and code. We need to compute the total distance Batman travels to retrieve all bombs.
The bombs are placed at distances (x, x^2, \dots, x^N).
He goes to each bomb and returns, so each bomb contributes (2 \cdot x^i).
Hence total distance (D = 2 \sum_{i=1}^{N} x^i = 2x \sum_{i=0}^{N-1} x^i).
We have to output (D \bmod M), where (M \le 10^5) (small).
(N) and (x) can be up to (10^9) – far too large for a loop.
Because (M) is small, we can work modulo (M) and compute the geometric sum
(S = \sum_{i=0}^{N-1} x^i \bmod M) using fast exponentiation with a simultaneous sum.
Recursive relation for geometric sum
Let (pow(n) = x^n \bmod M) and (sum(n) = \sum_{i=0}^{n-1} x^i \bmod M).
Base: (n = 0) → (pow = 1), (sum = 0).
For (n > 0):
Compute ((p, s)) for (k = \lfloor n/2 \rfloor).
Let (p2 = p \cdot p \bmod M).
If (n) is even ((n = 2k)):
[
pow(2k) = p2, \qquad sum(2k) = s \cdot (1 + p) \bmod M.
]
If (n) is odd ((n = 2k+1)):
[
pow(2k+1) = p2 \cdot x \bmod M, \qquad sum(2k+1) = \big(s \cdot (1 + p) + p2\big) \bmod M.
]
This recursion runs in (O(\log N)) per test case.
After obtaining (S = sum(N)), the answer is
[
\text{ans} = 2 \cdot (x \bmod M) \cdot S \bmod M.
]
Special cases speed up the code:
If (x \bmod M = 0) → every term (x^i) (for (i\ge 1)) is a multiple of (M), so answer (= 0).
If (x \bmod M = 1) → (x^i \equiv 1), hence (S = N \bmod M) and answer (= 2 \cdot (N \bmod M) \bmod M).
The algorithm handles up to (T = 50) test cases easily.
cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
pair<ll, ll> power_sum(ll x, ll n, ll M) {
if (n == 0) return {1 % M, 0};
auto [p, s] = power_sum(x, n / 2, M);
ll p2 = (p * p) % M;
if (n & 1) {
// odd: n = 2k+1
ll sum_val = (s * (1 + p) + p2) % M;
ll pow_val = (p2 * x) % M;
return {pow_val, sum_val};
} else {
// even: n = 2k
ll sum_val = (s * (1 + p)) % M;
return {p2, sum_val};
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while (t--) {
ll N, x, M; cin >> N >> x >> M;
ll xm = x % M;
if (xm == 0) {
cout << "0\n";
continue;
}
if (xm == 1) {
ll s = N % M;
cout << (2 * s) % M << "\n";
continue;
}
auto [_, s] = power_sum(xm, N, M); // we need only the sum
ll ans = (2 * ((xm * s) % M)) % M;
cout << ans << "\n";
}
return 0;
}