Given an integer N,Find how many strings of length N are possible, consisting only of characters { 'A','B' and 'C' } with each character {'A','B' and 'C'} occurs at least once.
Input:
First line of each test case contains number of test cases T. Each test case contains a single integer N.
Output:
For each test case print the expected output. Output may be too large so print it modulo 10^9+7.
Constraints:
1 ≤ T ≤ 10^5
1 ≤ N ≤ 10^9
SAMPLE INPUT
3
2
3
4
SAMPLE OUTPUT
0
6
36
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to compute the number of strings of length N over alphabet {A,B,C} such that each character appears at least once. This is the number of surjective functions from an N-set to a 3-set. The total number of strings is 3^N. The number where each appears at least once can be computed via inclusion-exclusion:
Count = total - (strings missing at least one specific character) + (strings missing at least two specific) - (strings missing all three). But since we have three characters, the formula:
f(N) = 3^N - 3 * 2^N + 3 * 1^N - 0^N.
But careful: 1^N = 1, and 0^N: if N>0, 0^N = 0; for N=0, it's 1 but N>=1 per constraints. So we can ignore 0^N.
Thus f(N) = 3^N - 3*2^N + 3.
We need to output modulo M = 10^9+7.
Now constraints: N up to 1e9, T up to 1e5. We need to compute for many N quickly. So we need to compute 3^N mod M and 2^N mod M efficiently using fast exponentiation (binary exponentiation) per test case? T up to 1e5, N up to 1e9, binary exponent O(log N) ~ 30 steps per exponent, so per test case maybe about 60 multiplications, which is 6e6 operations total, that's feasible. But maybe we can precompute powers? Since N is up to 1e9, we cannot precompute all. But 1e5 tests * 30 ~ 3e6 exponentiations? Actually each exponentiation is O(log N) modular multiplications. Two exponentiations per test: total ~ 21e530 = 6e6 multiplications, which is fine in 1 sec? In Python, 6e6 multiplications mod 1e9+7 is okay, maybe borderline but should be fine. But we also have input overhead. However we can optimize by using pow(base, N, mod) built-in which is fast in Python (C implementation). So we can just compute pow(3,N,mod) and pow(2,N,mod) for each test.
So answer is (pow(3,N,mod) - 3 * pow(2,N,mod) + 3) % mod. Need to ensure positivity: (a - b + mod) % mod.
But note: The formula is valid for N>=1. For N=0 (not in constraints) would be 1? Actually 0 length string with all three characters at least once impossible, but formula would give 1 - 3*1 + 3 =1, but 0^0? anyway not needed.
But wait, constraints: N can be up to 10^9, T up to 10^5. pow built-in is fast. However, we must be cautious: 3*pow(2,N,mod) could be large but mod ensures it's fine. Also add 3, then subtract.
Potential pitfalls: There's a mod, so we compute:
mod = 10**9+7
res = (pow(3,N,mod) - 3 * pow(2,N,mod) + 3) % mod
But sometimes if pow(2,N,mod) is large, 3 * that might overflow Python's integer? No, Python integers unlimited, but we can do modulo after multiplication. So best: (pow(3,N,mod) - (3 * pow(2,N,mod)) % mod + 3) % mod, but doing straightforward as Python will handle large but slower maybe? Actually modular multiplication of numbers less than mod yields product up to ~1e18, Python can handle. So fine.
But we need to process up to 1e5 test cases, so reading input efficiently: use sys.stdin.read().
Edge Cases: N = 0 not allowed. N large.
But is there any catch? The problem: "Given an integer N, Find how many strings of length N are possible, consisting only of characters { 'A','B' and 'C' } with each character {'A','B' and 'C'} occurs at least once." That's exactly surjective onto three. So formula holds.
But wait: Could there be any other restriction? The sample: N=2 ->0, N=3 ->6, N=4 ->36. Check: 3^4=81, 2^4=16, 81-48+3=36. Yes.
Thus solution straightforward.
But I see constraints T up to 1e5, N up to 1e9. So we need to be efficient. Using pow built-in is fine.
Now I should think: Could there be a way to precompute powers for all N? No. But maybe we can compute fast exponent per test. That's okay.
But also need to be careful: mod = 1000000007.
Now produce output for each test case on separate line.
Implementation plan:
import sys
data = sys.stdin.read().strip().split()
if not data: return
t = int(data[0])
Then iterate over next t numbers: for i in range(1, t+1): N = int(data[i])
compute ans = (pow(3,N,mod) - 3*pow(2,N,mod) + 3) % mod
store ans in list
print "\n".join(map(str, ans_list))
But note: The formula includes +3 at the end. However, if N is very large, pow(3,N,mod) and pow(2,N,mod) computed. Straight.
Alternatively, could use formula via combinatorics: 3! * S(N,3) where S(N,3) is Stirling number of second kind. But that's same.
Thus solution is simple.
But wait: The problem may have hidden twist: It says output may be too large so print modulo 10^9+7. That's what we did.
Now let's test for N=1: 0 mod? 0. N=2: 0. N=3: 6. N=4: 36. N=5: 3^5=243, 2^5=32 -> 243-96+3=150. Actually check manually? 150 seems plausible.
Now check with inclusion-exclusion: 3^5=243; strings missing A: 2^5=32, similarly missing B and C: 96 total; strings missing both A and B: 1^5=1, three pairs: 3; so inclusion-exclusion: 243 - 96 + 3 = 150. So correct.
Thus answer.
But constraints T up to 1e5, pow for each test will be two exponentiations. That's about 2e5 pow calls. Python's built-in pow with mod is implemented in C and is fast; 2e5 calls should be okay (~0.2 sec? Actually 2e5 * 30 steps = 6e6 multiplication operations in C, quite fast). But reading input and writing output also fine.
However, there is an alternative: compute 2^N mod M and 3^N mod M using precomputed exponentiation via binary exponent per test, but that's basically what pow does. So it's fine.
But wait: There's nuance: pow(base, exp, mod) uses exponentiation by squaring with modular reduction, O(log exp). So fine.
But T could be 1e5 and N up to 1e9, pow will handle.
We'll implement.
But check sample input: 3 lines, output as given.
Now, since I'm ChatGPT, I need to produce final answer explanation and code.
But before final, double-check if there is any other interpretation: Could the strings be considered with each character occurring at least once, but also perhaps order matters? Yes, that's what we did.
Thus solution is straightforward.
But perhaps they want to compute for large N using matrix exponentiation? No, formula is simple.
Thus code:
code
import sys
MOD = 10**9+7
def solve():
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
out = []
for i in range(1, t+1):
n = int(data[i])
a = pow(3, n, MOD)
b = pow(2, n, MOD)
res = (a - 3 * b + 3) % MOD
out.append(str(res))
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
solve()
Will this be fast enough? Yes.
But there is a possibility that we need to handle negative modulo: but Python's % yields positive.
Now one more optimization: since T up to 1e5 and maybe N can be repeated? But not stated. Could use caching but not necessary.
Now final output.
Thus solution.
But maybe we can also derive closed form but not needed.
Now also we should consider N=0 not in input but if appears, our formula with pow(2,0) =1, pow(3,0)=1 yields 1-3+3=1, which would be wrong, but constraints say 1 ≤ N, so fine.
Now one more thing: The formula uses 3 * 2^N, but careful: In inclusion-exclusion, we subtract number of strings missing at least one specific char: there are 3 choices of which char is missing, each count is 2^N (strings formed by remaining two chars). So correct. Then add back strings missing two specific chars: there are 3 choices of which two are missing (i.e., which one present), each count is 1^N = 1. So plus 3. Subtract strings missing all three: 0^N = 0 for N>0. So correct.
Thus final answer.
Now produce final response with explanation and code. We need to count strings of length N over the alphabet {A, B, C} such that each character appears at least once. The total number of strings is 3^N. Using inclusion-exclusion, the number of strings where all three characters appear is:
Since (1^N = 1) and for (N \ge 1), (0^N = 0), we have:
[
f(N) = 3^N - 3 \cdot 2^N + 3.
]
We must output the result modulo (10^9+7). The constraints allow up to (10^5) test cases and (N) up to (10^9). We can compute the required powers efficiently using Python's built-in pow(base, exp, mod), which performs modular exponentiation in (O(\log N)) time.
python
import sys
MOD = 10**9 + 7
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
out = []
for i in range(1, t + 1):
n = int(data[i])
a = pow(3, n, MOD)
b = pow(2, n, MOD)
res = (a - 3 * b + 3) % MOD
out.append(str(res))
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
solve()