Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish.
For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar").
For example:
For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z".
For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz".
For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz".
Since the answer can be quite large you should output it modulo 1000000007 (10^9 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :)
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N.
Output
For each test case, output a single line containing the answer for the corresponding test case.
Constrains
1 ≤ T ≤ 1000
1 ≤ N ≤ 10^9
Example
Input:
5
1
2
3
4
100
Output:
26
52
728
1404
508533804
Explanation
The first three examples are explained in the problem statement above.
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 palindrome strings of length ≤ N over lowercase English alphabet (26 letters). Length L: for each L, the number of palindromes is 26^{ceil(L/2)}? Actually, a palindrome of length L is determined by its first ceil(L/2) characters; each can be any of 26 letters. So count(L) = 26^{⌈L/2⌉}. But careful: L from 1 to N. So total = Σ_{L=1}^{N} 26^{⌈L/2⌉}.
We can rewrite in terms of k = ceil(L/2). For L odd: L = 2k-1, for k = 1,2,... up to ceil(N/2) maybe. For L even: L = 2k, for k = 1,2,... up to floor(N/2). So total = Σ_{k=1}^{⌈N/2⌉} count for odd lengths with that k? Actually for each k, there is at most one odd length (2k-1) and one even length (2k) if they are ≤ N.
So total = Σ_{k=1}^{⌊N/2⌋} 26^{k} (for even lengths) + Σ_{k=1}^{⌈N/2⌉} 26^{k} (for odd lengths?) Wait: For odd lengths: L=2k-1, ceil(L/2) = k. So each odd length contributes 26^k. For even lengths: L=2k, ceil(L/2)=k, contributes 26^k. So sum over L=1 to N 26^{ceil(L/2)} = (number of lengths L such that ceil(L/2) = k) * 26^k summed over k.
How many L in [1,N] have ceil(L/2)=k? That's: L satisfying ceil(L/2)=k => k = ceil(L/2) => k-1 < L/2 ≤ k => 2(k-1) < L ≤ 2k. Actually ceil(L/2)=k => (k-1) < L/2 ≤ k => 2k-2 < L ≤ 2k. But L integer, so L ∈ (2k-2, 2k] => L ∈ {2k-1, 2k}. However, L must be positive; for k=1, (0,2] => L=1,2. So each k (>=1) corresponds to exactly two lengths: 2k-1 (odd) and 2k (even), provided they are ≤ N. So for each k, if both 2k-1 and 2k ≤ N then both present, contribution 2*26^k. If N is odd, say N = 2M+1, then for k = M+1 (since ceil((2M+1)/2)=M+1) only the odd length (2(M+1)-1 = 2M+1) is present; the even length 2(M+1) > N. For k > M+1 none. If N is even, N=2M, then for k=M, both lengths 2M-1 and 2M are ≤ N? 2M-1 <= N, yes; 2M = N, also present. For k = M, both present. For k=M+1, the odd length would be 2(M+1)-1 = 2M+1 > N, even length 2M+2 > N, so none.
Thus we can compute total = Σ_{k=1}^{⌊N/2⌋} 2 * 26^k + (if N odd then + 26^{(N+1)/2} else 0). Because for N even, floor(N/2) = M, and there are M ks with both lengths, and no extra odd. For N odd, floor(N/2)=M, N=2M+1, then for k=1..M we have both lengths, and one extra odd length for k=M+1: 26^{M+1}.
Thus total = 2 * Σ_{k=1}^{⌊N/2⌋} 26^k + (N mod 2 ? 26^{(N+1)/2} : 0).
Now we need to compute this modulo MOD=1e9+7 for N up to 1e9, T up to 1000. So we need to compute geometric series of powers of 26 modulo MOD efficiently with exponent up to floor(N/2) which can be up to 5e8. That's large but we can use fast exponentiation and formula for sum of geometric series: S(m) = 26*(26^m - 1) / (26 - 1) modulo MOD, provided modular inverse of 25 exists (which it does, as MOD is prime and 25 not divisible by MOD). So S(m) = (26*(26^m - 1) * inv(25)) mod MOD. But careful: Σ_{k=1}^{m} 26^k = (26^{m+1} - 26) / (26 - 1) = (26^{m+1} - 26)/25. Alternatively, (26*(26^m - 1))/25. Both work.
So we need to compute 26^m mod MOD, where m = floor(N/2) (for the sum) and possibly also 26^{(N+1)/2} for odd N. But m can be up to 5e8, but fast exponentiation O(log m) per test case, with T=1000, that's 1000log2(5e8) ~ 100029 = 29k operations, fine. However, using Python's pow(base, exp, mod) is efficient. So we can compute:
m = N // 2
sum_pow = (26 * (pow(26, m, MOD) - 1) * inv25) % MOD // but careful: (pow(26,m)-1) might be negative mod, so add MOD.
But check: Σ_{k=1}^{m} 26^k = (26*(26^m - 1) * inv25) mod MOD. Since we have division by 25, compute inv25 = pow(25, MOD-2, MOD) once.
Then total = (2 * sum_pow) % MOD. If N odd: total = (total + pow(26, (N+1)//2, MOD)) % MOD.
But we should double-check for N=1: N=1 => m=0, sum_pow = Σ_{k=1}^{0} = 0. Then total = 2*0 =0, plus odd extra: pow(26, (1+1)//2 =1) =26, total=26 correct.
N=2: N even, m=1, sum_pow = Σ_{k=1}^{1} 26^1 =26. But using formula: pow(26,1)=26, (26*(26-1)=2625=650, inv25=?? 1/25 mod? Actually 650inv25 mod: 650/25=26, correct. Then total=2*26=52. No extra => 52 correct.
N=3: N odd, m=1, sum_pow =26, total = 2*26=52, plus extra pow(26,(3+1)//2=2) = 26^2=676, total=728 correct.
Now we need to consider that N can be up to 1e9, so exponent for pow is up to 5e8, but Python's built-in pow with three arguments uses modular exponentiation and is efficient. However, for T=1000, computing 1000 such pow calls may be acceptable; each pow is O(log exponent) ~ 30 multiplications. So total ~ 30000 multiplications, fine.
But careful: for m=0, pow(26,0,MOD)=1, then (26*(1-1)=0, sum=0. So handle fine.
Edge Cases: N very large. Also we need to consider that modular arithmetic may produce negative after subtraction; we can add MOD before multiplying.
Implementation:
MOD = 1000000007
inv25 = pow(25, MOD-2, MOD) (since MOD is prime and 25<MOD)
For each test case:
read N
m = N // 2
compute sum1 = sum_{k=1}^{m} 26^k
if m == 0:
sum1 = 0
else:
pow26_m = pow(26, m, MOD)
sum1 = (26 * (pow26_m - 1) % MOD) * inv25 % MOD
total = (2 * sum1) % MOD
if N % 2 == 1:
total = (total + pow(26, (N+1)//2, MOD)) % MOD
print(total)
But double-check the formula for sum of geometric series: Σ_{k=1}^{m} r^k = (r^{m+1} - r) / (r-1). Since r=26. Let's test: m=2: r^3=17576, minus r=26 => 17550, divide by 25 = 702, correct. Our formula: 26*(26^m - 1)/25 = 26*(676-1)/25 = 26675/25 = 2627=702. Works. So using 26*(pow26_m -1) /25 is correct. But we need to ensure the division is exact in integer arithmetic before mod? It will be exact because we use modular inverse and mod prime; as long as we treat as modulo it's fine.
Now complexity: O(T log N). Good.
However, there is one nuance: For large m, pow(26,m,MOD) computed quickly, but we might also need to compute pow(26, (N+1)//2, MOD) which is about same exponent. So up to 2 pow per test case. That's okay.
We should also consider if we can precompute something? Not necessary.
Now also we need to be careful about integer overflow? Python big ints fine.
Now final answer.
But wait: The problem statement may require to compute modulo 10^9+7. Yes.
Edge: For N = 10^9, m=5e8, pow modular exponentiation is fine.
Now I'll produce the Python code.
But we must read T test cases. Input may be large but T=1000, fine.
I'll write:
import sys
MOD = 1000000007
INV25 = pow(25, MOD-2, MOD)
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
T = int(next(it))
out = []
for _ in range(T):
N = int(next(it))
m = N // 2
if m == 0:
sum_pow = 0
else:
pow26_m = pow(26, m, MOD)
sum_pow = (26 * (pow26_m - 1) % MOD) * INV25 % MOD
total = (2 * sum_pow) % MOD
if N & 1:
total = (total + pow(26, (N+1)//2, MOD)) % MOD
out.append(str(total))
sys.stdout.write("\n".join(out))
if name == "main":
solve()
But wait: There's a potential off-by-one: For N even, N=2, m=1, sum_pow = 26, total=52, extra none => 52 ok. For N=0? N>=1, so fine.
Now, test with sample: N=100? We'll compute output: sample says 508533804. Let's test quickly using code mentally? Hard but trust.
One more check: For N=4, m=2, pow26_m=676, sum_pow = 26*(676-1)=26675=17550; 17550inv25 mod? Since 17550/25=702, so sum_pow=702; total=1404; even => 1404 correct.
For N=5: N odd, m=2, sum_pow=702, total base=1404; extra = 26^{(5+1)//2}=26^3=17576; total=1404+17576=18980? Wait compute: 26^3=17576? 26^2=676, *26=17576 correct. So 1404+17576=18980. Let's manually compute: lengths: L=1:26, L=2:26, L=3:26^2=676, L=4:26^2=676? Wait count(L) = 26^{ceil(L/2)}. L=1:26^1=26; L=2:26^1=26; L=3:26^2=676; L=4:26^2=676; L=5:26^3=17576. Sum=26+26+676+676+17576 = 18980. Yes matches.
Now, we can also compute using direct formula for N odd: total = 2* Σ_{k=1}^{M} 26^k + 26^{M+1} with M = floor(N/2). For N=5, M=2, 2*(26+676)=2*702=1404, + 26^{3}=17576, sum=18980.
All good.
Now, note that the modulo may cause negative after subtraction (pow26_m - 1) if pow26_m = 0? pow26_m can't be 0 mod MOD because 26 mod MOD is not zero. So pow26_m >=1, but after modulo it could be 0 if 26^m ≡ 1 mod MOD? Actually 26^m mod MOD could be 1 if m is multiple of order; but possible? Since MOD is prime, 26 is not 0 mod MOD, order divides MOD-1=1e9+6. It's possible that for some m, 26^m ≡ 1 mod MOD. Then pow26_m -1 = 0 mod MOD, fine. So negative not an issue if we do (pow26_m - 1) % MOD? But in Python, pow(...) returns a number in [0, MOD-1]. So pow26_m - 1 may be -1? Actually pow returns non-negative less than MOD. So if pow26_m = 0, it's not possible because 26^m mod prime cannot be 0. So pow26_m is in [1, MOD-1]. Then pow26_m - 1 is in [0, MOD-2]. So it's non-negative. However, to be safe, we can do (pow26_m - 1 + MOD) % MOD but not needed because it's non-negative. But to maintain positivity after multiplication we can just compute:
tmp = (pow26_m - 1) * 26 % MOD
But better to do: sum_pow = ((pow26_m - 1) * 26 % MOD) * INV25 % MOD
But we should be careful that (pow26_m - 1) * 26 may overflow? Python big ints, fine.
Now I think it's solved.
Potential pitfalls: For m=0, we skip pow calculation. Good.
Now we need to consider if we need to use fast I/O? Since T up to 1000, it's fine.
Now produce final answer.
But before finalizing, we should check if there is any other nuance: The problem asks for strings of length not exceeding N, containing only lowercase letters. The examples match. So it's correct.
Now we can finalize.
One more thing: The sum formula using modular inverse of 25 works because MOD is prime and 25 not divisible by MOD. So yes.
Alternatively, we could compute using geometric series formula but with exponentiation and modular inverse.
Now code.
However, note that for large T (1000) and each pow may be heavy but fine. Actually pow in Python uses exponentiation by squaring in C, so very fast. So it's okay.
Now, I'll output solution.
But the problem may have originally been from CodeChef, and often they expect solutions that handle large exponent via exponentiation. So this is standard.
Now, I'll provide the explanation as required.
But the user asked: "Do you know that The Chef has a special interest in palindromes? ..." They want solution. So I will respond with the solution and code.
I'll produce final answer: Explanation of formula and Python code.
Now I'll produce final response.
```python
import sys
MOD = 1000000007
INV25 = pow(25, MOD - 2, MOD) # modular inverse of 25 modulo MOD
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
m = n // 2 # floor(n/2)
code
# sum_{k=1}^{m} 26^k
if m == 0:
sum_pow = 0
else:
pow26_m = pow(26, m, MOD)
# 26 * (26^m - 1) / 25 (mod MOD)
sum_pow = (26 * (pow26_m - 1) % MOD) * INV25 % MOD
total = (2 * sum_pow) % MOD
if n & 1: # n is odd
total = (total + pow(26, (n + 1) // 2, MOD)) % MOD
out_lines.append(str(total))
sys.stdout.write("\n".join(out_lines))