We are to compute the number of strings t (over alphabet {A, C, G, T}) of length n such that the Vasya distance ρ(s, t) is maximum possible. The function ρ(s, t) is defined as sum over i from 0 to n-1 (cyclic shifts of s) and j from 0 to n-1 (cyclic shifts of t) of h(shift_i(s), shift_j(t)), where h counts matches per position.
Given s of length n, we need to maximize ρ(s, t) over all t of length n, and count how many t achieve that maximum, modulo 1e9+7.
Let's denote: s is fixed, length n. For any t, we need to compute:
ρ(s, t) = sum_{i=0}^{n-1} sum_{j=0}^{n-1} h( shift_i(s), shift_j(t) ).
This is like sum over all pairs of cyclic shifts of s and t of the number of positions where they match.
Define S_i = shift_i(s) (left circular shift i times). Similarly T_j = shift_j(t). Then:
ρ = sum_{i,j} h(S_i, T_j).
We can reinterpret.
Let’s denote strings as sequences of characters. h(x,y) = number of k such that x_k = y_k.
So ρ = sum_{i,j} sum_{k=0}^{n-1} [ S_i[k] = T_j[k] ].
Swap sums: ρ = sum_{k=0}^{n-1} sum_{i,j} [ S_i[k] = T_j[k] ].
Now S_i[k] is the character of s at position (i + k) mod n? Let's define indexing: s[0..n-1] original. A left cyclic shift by i means: shift_i(s)[k] = s[(i+k) mod n]? Typically left shift: if we shift left by 1, the first character moves to end. For original s = s0 s1 ... s_{n-1}. After left shift i, the string becomes s_i s_{i+1} ... s_{n-1} s_0 ... s_{i-1}. So at position k (0-indexed), the character is s[(i+k) mod n]. Yes.
Similarly, T_j[k] = t[(j+k) mod n].
So the condition S_i[k] = T_j[k] becomes s[(i+k) mod n] = t[(j+k) mod n].
Let’s change indices. For a fixed offset d? Let's set a = (i+k) mod n, b = (j+k) mod n. Then condition s[a] = t[b]. But a and b are both expressed in terms of i, j, k. We can think of summing over all i, j, k. Perhaps we can reparameterize to reduce.
Let’s define for each pair (i,j), consider all positions k. Equivalent to considering all triples (i,j,k). We can set u = (i+k) mod n, v = (j+k) mod n. As i varies over 0..n-1, j varies 0..n-1, k varies 0..n-1. For each fixed (u,v), how many triples (i,j,k) yield that (u,v)? We have u = (i+k) mod n => i = (u - k) mod n; similarly j = (v - k) mod n. For each (u,v,k), i and j are determined uniquely mod n. So mapping is a bijection: (i,j,k) <-> (u,v,k) via the above. Since i, j, k each range over n values, total n^3 triples. The mapping (i,j,k) -> (u,v,k) is bijective because for each k, i determines u and v from j, but we can invert: given u,v,k, i = u - k mod n, j = v - k mod n. So it's a bijection. So:
ρ = sum_{i,j,k} [ s[(i+k) mod n] = t[(j+k) mod n] ]
= sum_{u=0}^{n-1} sum_{v=0}^{n-1} sum_{k=0}^{n-1} [ s[u] = t[v] ]? Wait careful: under the mapping, u = (i+k) mod n, v = (j+k) mod n, and k stays k. So for each u,v,k we have a term [ s[u] = t[v] ] but note that the original condition depends on u and v through s[u] and t[v] and does not involve k. So for each (u,v) pair, the condition s[u] = t[v] is either true or false; and for each such pair, the number of k that correspond? In the sum, we have one term for each triple (i,j,k). After reparameterization, we get one term for each triple (u,v,k) as well. But the condition depends only on u,v, not on k. So we can write:
ρ = sum_{u=0}^{n-1} sum_{v=0}^{n-1} (number of k such that there exist i,j mapping? Actually each (u,v,k) yields a term; for each (u,v,k) we have [ s[u] = t[v] ]. So overall:
ρ = ∑{u=0}^{n-1} ∑{v=0}^{n-1} ∑{k=0}^{n-1} [ s[u] = t[v] ] = n * ∑{u,v} [ s[u] = t[v] ].
But careful: the mapping from (i,j,k) to (u,v,k) is a bijection, so indeed each (u,v,k) appears exactly once. So:
ρ = ∑{u=0}^{n-1} ∑{v=0}^{n-1} ∑{k=0}^{n-1} I( s[u] == t[v] ) = n * ∑{u=0}^{n-1} ∑_{v=0}^{n-1} I( s[u] == t[v] ).
Thus ρ = n * M, where M = number of ordered pairs (u, v) (positions in s and t) such that s[u] = t[v].
That's a neat simplification! Let's verify with small example: n=1: s = "C". Then M = I(s[0]==t[0]) = 1 if t[0]=C else 0. So ρ = 1 * 1 =1 for t="C", else 0. Matches example: ρ=1 for t="C", 0 for others.
For n=2: s="AG". Let's compute ρ for t="AG": s[0]=A, s[1]=G. t[0]=A, t[1]=G. So M = (A==A)+(A==G?)+(G==A?)+(G==G) =1+0+0+1=2. Then ρ = 2 * 2 =4. The example says ρ("AG","AG")=4. Good.
t="GA": t[0]=G, t[1]=A. M = (A==G?)+(A==A?)+(G==G?)+(G==A?) =0+1+1+0=2 => ρ=4. t="AA": M = (A==A)+(A==A? Actually t[0]=A,t[1]=A) => pairs: s0=A vs t0=A:1, s0=A vs t1=A:1, s1=G vs t0=A:0, s1=G vs t1=A:0 => M=2, ρ=4. t="GG": M = (A==G?0)+(A==G?0)+(G==G?1)+(G==G?1)=2 => ρ=4. So all four t have ρ=4. Maximum? Could there be t with M=4? For n=2 maximum M is when t such that every s[u] equals t[v]? That would require s constant? Actually max possible M = n^2? Since M is number of pairs (u,v) with equality. Max is when s and t both constant and same letter? Then M=n*n =4 => ρ=8. But is that achievable? For s="AG", can we pick t such that s[u] == t[v] for all u,v? That would require t[0] equals both A and G simultaneously, impossible. So max M is less. We need to compute maximum possible M over t, given s. And then count t achieving that M. Since ρ = n * M, maximizing ρ is equivalent to maximizing M.
Thus the problem reduces: given string s of length n over alphabet Σ={A,C,G,T}, we need to maximize over strings t (also length n over Σ) the number of ordered pairs (u, v) with s[u] = t[v]. Then count t achieving that maximum, modulo MOD=1e9+7.
Let’s denote for each character c ∈ Σ, let a_c = frequency of c in s (i.e., number of positions u where s[u]=c). Similarly, for t, let b_c = frequency of c in t. Then M = ∑_{c} a_c * b_c. Because for each character c, each position where s has c and t has c contributes 1 for each such pair: number of pairs (u,v) with s[u]=c and t[v]=c = a_c * b_c.
Thus M = ∑_{c ∈ Σ} a_c * b_c. The vector a is fixed. We can choose t, i.e., choose nonnegative integers b_c summing to n (the frequencies of t). Then M = a·b.
We need to maximize a·b subject to b_c ≥ 0 integers, sum b_c = n. And also b_c correspond to counts; no further constraints (any distribution of characters is possible). So it's a linear optimization: given fixed a (a vector in ℝ^4), maximize dot product with b subject to b nonnegative integer and sum n. Since a are nonnegative integers, the maximum is achieved by putting all n into the character(s) with largest a_c. Because a·b = ∑ a_c b_c ≤ (max_c a_c) * ∑ b_c = max_c a_c * n, with equality if we set b_c nonzero only for those c achieving the maximum a_c. More precisely, let A_max = max_{c} a_c. Then M_max = A_max * n. Because we can set b_c = n for a character c* with a_c* = A_max, and b other =0. Then M = a_c* * n = A_max * n. That is maximum because for any b with sum n, a·b ≤ (max a_c) * sum b_c = A_max * n.
Thus the maximum Vasya distance ρ_max = n * M_max = n * (A_max * n) = n^2 * A_max? Wait ρ = n * M, and M_max = A_max * n, so ρ_max = n * (A_max * n) = A_max * n^2. That seems plausible. Let's test: For s="AG", a_A=1, a_G=1, A_max=1, n=2, ρ_max = 1 * 4 =4, matches earlier where we got ρ=4 for several t. Yes, because maximum M is 2? Wait A_max=1, n=2, M_max=2, ρ_max =2*2=4. Yes.
For s="TTT", a_T=3, other 0, A_max=3, n=3, M_max=33=9? That would be 9, ρ_max = n * M_max = 39=27. Example says ρ("TTT","TTT")=27, and only one t yields that. Indeed the maximum is achieved by t = all T's, which gives b_T = 3, M = a_T * b_T = 33=9, ρ=27. But could there be other t? If we set b_T = n =3, then M=9. For any other distribution, say b_T=2, b_other=1, then M = 32 + 0*1 =6, less than 9. So indeed only t where all characters are the one(s) with max frequency achieve M_max. However, if multiple characters tie for max frequency, then any t that uses only those characters (with any frequencies) would yield M = a_c1 * b_c1 + a_c2 * b_c2 + ... But note a_c are equal for those characters (equal to A_max). So if we restrict to a subset C_max = {c: a_c = A_max}, then for any t with b_c nonzero only for c in C_max, we have M = ∑_{c in C_max} A_max * b_c = A_max * (∑ b_c) = A_max * n, because sum of b_c = n. So any t that consists exclusively of characters from the set of most frequent letters in s will achieve M_max. But careful: does any distribution among those max letters work? Yes, because a_c are equal to A_max for those letters, so the dot product becomes A_max times total count of those letters, which is n. So indeed any t with characters only from C_max yields M = A_max * n, which is maximal.
But wait, is it necessary that all characters of t belong to C_max? What if we include a character d not in C_max? Then b_d > 0 and for that d, a_d < A_max, while some b_c for c in C_max will be less than n. Since we have total sum n, if we allocate x to d, then the sum of b_c for c in C_max is n - x. Then M = A_max*(n-x) + a_d * x. Since a_d ≤ A_max - 1 (since a_d is integer and less than A_max if not maximum). So M = A_max*n - (A_max - a_d)x < A_maxn if x > 0. So indeed to achieve maximum, we must have b_d = 0 for all d not in C_max. So t must consist entirely of characters from C_max.
Thus the set of t achieving maximum is: all strings of length n over the alphabet C_max (subset of {A,C,G,T}) of size m = |C_max|, where m = number of letters that appear with maximum frequency in s. Count = m^n modulo MOD. However, is that always correct? Let's test examples.
Example1: n=1, s="C". a_C=1, a_A=a_G=a_T=0. A_max=1, C_max = {C}. m=1 => count = 1^1 =1. matches.
Example2: n=2, s="AG". a_A=1, a_G=1, a_C=0, a_T=0. A_max=1, C_max = {A,G}. m=2 => count = 2^2 =4. matches.
Example3: n=3, s="TTT". a_T=3, others 0, A_max=3, C_max={T}, m=1 => count =1^3=1. matches.
So seems simple. But need to check edge cases: if there are multiple letters with same maximum frequency, yes.
But wait, is there any subtlety about the definition of ρ? We derived ρ = n * ∑{u,v} I(s[u]=t[v]) = n * (a·b). That derivation seemed solid, but we should double-check for potential off-by-one or misinterpretation of shift indices. Also ensure that we considered all cyclic shifts of both strings. The original definition: ρ(s, t) = sum{i=0}^{n-1} sum_{j=0}^{n-1} h(shift_i(s), shift_j(t)). Our derivation used i,j from 0 to n-1. For each i,j, h is number of matches at positions k from 0 to n-1. So yes.
Now the mapping: (i,j,k) → (u = (i+k) mod n, v = (j+k) mod n, k). But note that we sum over i,j,k. The mapping (i,j,k) → (u,v,k) is bijection because for each k, the mapping from (i,j) to (u,v) is bijective: u = (i+k) mod n, v = (j+k) mod n, so i = (u - k) mod n, j = (v - k) mod n. So it's a bijection. So indeed each triple (i,j,k) corresponds to exactly one triple (u,v,k), and vice versa. So sum over i,j,k [s[(i+k) mod n] = t[(j+k) mod n]] = sum over u,v,k [s[u] = t[v]] = n * ∑_{u,v} I(s[u]=t[v]).
Thus it's correct.
Now maximize M = ∑_c a_c b_c. The maximum is indeed A_max * n, achieved if and only if b_c = 0 for all c with a_c < A_max. And b_c can be any nonnegative integers summing to n as long as they are zero outside C_max. So t must be composed exclusively of characters from C_max. So total number = |C_max|^n.
But is there any additional constraint? The t strings are of length n; we can assign each position independently any character from C_max. So count = m^n.
However, we must check if there could be any other t achieving the same M even if using non-max characters? Let's examine scenario where some a_c equal to A_max, and some other a_d also equals A_max? That's already included. What about case where a_d = A_max but not? If it's less, we already argued it's suboptimal unless A_max - a_d = 0. So only equal.
Thus answer = (number of letters that achieve maximum frequency in s)^n mod MOD.
But wait, is there any scenario where A_max=0? That would happen if s is empty? But n>=1, so a_c sum to n>0, so A_max>0.
Now the problem statement: "Vasya found a string s of length n on the Internet. Now he wants to count how many strings t there are such that the Vasya distance from the string s attains maximum possible value." According to our derivation, answer = m^n.
But I recall that in some Codeforces problem (maybe problem "Vasya and DNA" or similar) there was a twist: the maximum might be achieved also for strings t that are not necessarily over C_max? Let's double-check more thoroughly.
We need to maximize M = a·b, with b nonnegative integers sum to n. This is a linear programming over integers. Since it's a knapsack-like, the maximum is achieved at an extreme point: allocate all to a maximum coefficient. So indeed b must be zero for all with coefficient less than max. So the condition is necessary and sufficient.
Thus answer is m^n.
But is there any other nuance: The distance ρ is defined over all cyclic shifts, but we derived it to n * M. Could there be a t that yields higher ρ by some different arrangement beyond just frequency counts? No, because we computed ρ exactly in terms of frequencies. The expression is exact. So the max is as described.
Thus the problem seems trivial? But the constraints are n up to 1e5, and alphabet size 4. So we can compute frequencies of A, C, G, T in s, find max frequency count, compute pow(m, n, MOD). That's O(n) time.
But wait, the problem might be more complicated because of cyclic nature. Could there be any t such that ρ(s, t) is larger than n * A_max * n? Let's verify with an example. Suppose s = "AC". a_A=1, a_C=1, A_max=1. Then our predicted max ρ = n^2 * A_max? Actually ρ = n * M, M_max = A_max * n = 12=2, so ρ_max=4. Can we find a t with ρ > 4? Try t = "AA". Then M = a_Ab_A + a_Cb_C = 12 + 10 =2, ρ=4. t="AC": M=11+11=2 ρ=4. t="CA": M=11+11=2 ρ=4. t="CC": M=0? Actually a_A=1, a_C=1; t="CC": b_A=0,b_C=2 => M=10+12=2 ρ=4. So indeed max 4. So consistent.
What about s = "AAC" (n=3). a_A=2, a_C=1, max=2. Then M_max = 23 =6? Actually A_max=2, n=3 => M_max=23=6, ρ_max = nM_max=36=18. Does that hold? t = "AAA" gives b_A=3, M = a_Ab_A =23=6, ρ=18. t = "AAC" gives b_A=2,b_C=1 => M=22+11=5 => ρ=15. t="ACA": b_A=2,b_C=1 same. t="CCC": b_A=0,b_C=3 => M=13=3, ρ=9. So max is 18. Can any other t give M=6? For M=6 we need 2b_A + 1*b_C =6 with b_A+b_C=3. Solve: 2b_A + (3-b_A)=6 => b_A +3 =6 => b_A=3, b_C=0. So only t with all A's. So count = 1^3 =1. So if only one max letter, count = 1. Good.
Now consider s where multiple max: s="AACC" (n=4). a_A=2, a_C=2, others 0, max=2. Then M_max = 24 =8. Achievable by any t composed of A and C only? Let's test: t = "AAAA": b_A=4, b_C=0 => M = 24 =8. t="ACAC": b_A=2,b_C=2 => M=22+22=8. t="CCCC": M=24=8. t="AAAC": b_A=3,b_C=1 => M=23+21=8. Yes, any combination of A and C gives M=2(b_A+b_C)=2*4=8. So any t over {A,C} works. So count = 2^4 =16. That seems plausible.
Thus answer is indeed m^n.
But wait, there might be a nuance: we assumed that the sum over u,v I(s[u]=t[v]) equals a·b. However, is there any effect due to the cyclic shifts? Because we derived ρ = n * ∑{u,v} I(s[u]=t[v]). This is correct for any t. So maximizing ρ is equivalent to maximizing ∑{u,v} I(s[u]=t[v]), which is a·b.
Therefore answer is m^n.
But let's test with an additional small example by brute force for n=2, s="AA". a_A=2, others 0, max=2, m=1, count=1^2=1. Let's compute manually: ρ(s,t) for s="AA". For t="AA": s shifts: "AA","AA". t shifts: "AA","AA". h for each pair? h("AA","AA")=2. There are 2*2=4 pairs, each h=2 => ρ=8. For t="AC": t shifts: "AC","CA". Compute h: S0="AA" vs T0="AC": matches at pos0: A==A yes, pos1: A==C no =>1. S0 vs T1="CA": A==C?0, A==A?1 =>1. S1="AA" vs T0="AC":1, vs T1="CA":1. So total ρ=4. For t="CC": h? S="AA" vs "CC":0 each =>0, but also vs "CC"? Actually T shifts: "CC","CC". All h=0 => ρ=0. For t="CA": similar to AC gives 4. So only "AA" gives max=8. So count=1. matches.
For s="AT": a_A=1,a_T=1, max=1,m=2, count=2^2=4. Let's brute: s="AT", all t of length 2 over {A,T}? Actually we can include others? But expected max achieved by t composed of A,T only. Let's compute ρ for each t: t="AA": M = a_Ab_A + a_Tb_T =12+10=2 => ρ=4. t="AT": M=11+11=2 =>4. t="TA": M=11+11=2 =>4. t="TT": M=0+12=2 =>4. So indeed 4. t="AC": M=11+0? a_T=1, b_T=0 => M=1 => ρ=2? Actually M = a_Ab_A + a_Tb_T + a_Cb_C + a_Gb_G = 11 (A) + 10 (T) + 0*1 (C) +0 =1 => ρ=2. So less. So maximum only achieved by t over {A,T}. So 4 t's. Good.
Thus solution appears correct.
But wait, there is a nuance: The Vasya distance is defined as sum over i,j of h(shift_i(s), shift_j(t)). In our derivation we considered i and j from 0 to n-1. But is it exactly that? The problem statement says: "where is obtained from string s, by applying left circular shift i times." It does not restrict i and j? In the sum notation, they show two summation signs from i=1 to n? Actually they wrote:
where is obtained from string s, by applying left circular shift i times.
In the description, they wrote:
ρ(s, t) = Σ_{i=0}^{n-1} Σ_{j=0}^{n-1} h(s_i, t_j)
with s_i being left circular shift i times. So yes.
Thus answer = (number of letters with max frequency)^n mod MOD.
But is there any scenario where the maximum value can be achieved also by t that are not composed solely of max letters due to some property of circular shifts? Our derivation shows ρ is exactly n * a·b. So no.
Double-check derivation with an alternative method: Compute ρ(s, t) = Σ_{i,j} h(shift_i(s), shift_j(t)). We can express h = Σ_{k} [s[(i+k) mod n] = t[(j+k) mod n]]. Summing over i,j,k yields Σ_{k} Σ_{i,j} [s[(i+k) mod n] = t[(j+k) mod n]]. For each fixed k, the set of (i,j) runs over all pairs. For each k, consider substitution i' = (i+k) mod n, j' = (j+k) mod n. As (i,j) runs over all pairs, (i',j') also runs over all pairs, because addition of constant modulo n is bijection. So inner sum = Σ_{i',j'} [s[i'] = t[j']], independent of k. Hence total = Σ_{k} ( Σ_{i',j'} [s[i'] = t[j']] ) = n * Σ_{i',j'} [s[i'] = t[j']]. That's the same.
Thus robust.
Now, the problem is quite simple. But maybe there is a catch: The string s might be up to 1e5, so computing m^n modulo MOD is fine using fast exponentiation.
But wait: the answer is m^n. But if m = 4 (all frequencies equal), then answer = 4^n, which could be huge but mod okay. That seems plausible.
But I recall a Codeforces problem titled "DNA Sequence" or similar, where answer is power of number of most frequent letters. But that problem might have had additional twist about cyclic equivalence? However, the note says: "Please note that if for two distinct strings t1 and t2 values ρ(s, t1) и ρ(s, t2) are maximum among all possible t, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one." That's consistent with our counting; we count each string separately.
Thus final solution: read n, read s. Count frequencies of A, C, G, T. Determine max frequency. Count how many letters have that frequency (i.e., m). Compute pow_mod(m, n, MOD). Print.
But before concluding, let's test some random cases manually with small n and all possible t to ensure no oversight.
We'll do a brute force for n=3 with various s.
Case s = "ACG": frequencies: A:1, C:1, G:1, T:0 -> max=1, m=3 (A,C,G). Then according to formula, answer = 3^3 = 27. Let's compute ρ_max = n^2 * A_max = 9 1 =9? Actually ρ_max = n * M_max, M_max = A_max * n = 13=3, so ρ_max = 33=9. So maximum ρ is 9. All strings t over {A,C,G} should give ρ=9. Let's check some: t="AAA": M = a_Ab_A + a_Cb_C + a_Gb_G =13+0+0=3 => ρ=9. t="AAC": b_A=2,b_C=1 => M=12+11+10=3 => ρ=9. t="ACT": includes T, b_T=1, b_A=1,b_C=1 => M = a_A1 + a_C1 + a_G0 + a_T1? a_T=0 so M=1+1+0=2 <3 => ρ=6. So indeed only strings over {A,C,G} achieve max. Number of strings over {A,C,G} of length 3 = 27. So answer 27. Good.
What about s = "AAT": frequencies: A:2, T:1, others 0. max=2, m=1 (A). answer = 1^3 =1. Only t="AAA"? Wait, also t="AAT"? Let's compute: t="AAA": b_A=3 => M=23=6 => ρ=18. t="AAT": b_A=2,b_T=1 => M=22+11=5 => ρ=15. So only "AAA" achieves max? Check "ATA": b_A=2,b_T=1 => same 5. "TAA": same. "TTT": 13=3. So yes, only "AAA". So answer 1.
Now s = "AATT": n=4, a_A=2, a_T=2, max=2, m=2, answer=2^4=16. Check t="AATA": b_A=3,b_T=1 => M=23+21=8 => ρ=32? Actually ρ = n * M = 48=32. t="AAAA": M=24=8 => ρ=32. t="ATAT": b_A=2,b_T=2 => M=22+22=8 => ρ=32. So all strings over {A,T} yield max. Count = 2^4=16. Good.
Thus answer consistent.
But wait: there is also possibility that the maximum frequency occurs for multiple letters, but also includes letters with zero frequency? No, zero frequency is less than max (unless max=0, impossible). So only letters with actual frequency equal to max.
Edge case: what if there are multiple letters with same max, but one of them has frequency equal to max but maybe we could also include a letter with lower frequency if we compensate by increasing others? But we already argued that since dot product is linear and the coefficients are constant, allocating any amount to a lower coefficient reduces total for a given sum, because you replace a unit of max coefficient with a unit of lower coefficient. So not.
Thus answer stands.
Now I'm thinking: Is there any scenario where the maximum M is not simply A_max * n due to integrality? Suppose we had to allocate n characters but the coefficient for the max letters is A_max, but maybe we could get higher by mixing if there is some interaction with the cyclic shift? But we derived exact expression, no interaction.
But wait: maybe our derivation that ρ = n * Σ_{u,v} I(s[u]=t[v]) is wrong? Let's re-derive carefully with indices:
Define s0...s_{n-1}, t0...t_{n-1}.
Shift_i(s) = s_{i} s_{i+1} ... s_{i-1} (left shift). So at position k (0-indexed), shift_i(s)[k] = s_{(i+k) mod n}.
Similarly shift_j(t)[k] = t_{(j+k) mod n}.
Then h(shift_i(s), shift_j(t)) = Σ_{k=0}^{n-1} [ s_{(i+k) mod n} = t_{(j+k) mod n} ].
Thus ρ(s,t) = Σ_{i=0}^{n-1} Σ_{j=0}^{n-1} Σ_{k=0}^{n-1} [ s_{(i+k) mod n} = t_{(j+k) mod n} ].
Now change variable: for each fixed k, define u = (i+k) mod n, v = (j+k) mod n. As i ranges 0..n-1, u ranges 0..n-1 (bijection). Similarly for j and v. So for each fixed k, the double sum over i,j of the indicator is Σ_{u=0}^{n-1} Σ_{v=0}^{n-1} [ s_u = t_v ]. That's independent of k. And there are n choices for k. So indeed ρ(s,t) = Σ_{k=0}^{n-1} ( Σ_{u,v} [ s_u = t_v ] ) = n * Σ_{u,v} [ s_u = t_v ].
Thus correct.
Thus answer is simple.
But wait: the problem may be more interesting: The input constraints n up to 1e5, string s of length n. So solution O(n) is fine.
But I suspect many participants might get stuck on thinking about cyclic convolution, etc. But indeed answer is straightforward.
Now, let's confirm modulo 1e9+7.
Implementation: read n, s. Count frequencies for each char: A, C, G, T. Determine max. Count number of chars whose frequency equals max. Let m be that count. Compute pow(m, n, MOD). Print.
But careful: The answer is the number of strings t achieving maximum distance. Could there be any string t that is not counted because it uses only max letters? All such t achieve maximum. But is it possible that some t that includes a non-max letter also achieves the same M? We argued no. But consider if A_max = 0? No. So fine.
Double-check with an example where max frequency is 0? Not possible.
Now, think about scenario where n is 1, s is "A". Then a_A=1, others 0, max=1, m=1, answer=1. Indeed only "A" yields ρ=1. But also note that the definition includes all shifts. For n=1, shifting doesn't change string, so ρ(s,t) = h(s,t) * 1? Actually sum over i=0 to 0, j=0 to 0 => one h. So ρ = h(s,t). That's 1 if t="A", else 0. So maximum 1, only one t. Good.
Now, could there be any scenario where some t composed of max letters does not yield maximum because of something about shifts? That would contradict our derivation. But maybe our derivation missed the fact that for each (u,v) we counted n times, but is it possible that some (u,v) pairs are overcounted or something? Let's test with a specific s and t to compute ρ via our formula and compare to direct enumeration for small n.
Take n=3, s="ABC"? but alphabet is ACGT only. Let's use ACGT. s="ACT", t="CTA"? But both consist of letters in C_max? Let's pick s="ACT": a_A=1,a_C=1,a_G=0,a_T=1 -> max=1, m=3 (A,C,T). According to formula, any t over {A,C,T} should give M=3, ρ=9. Let's pick t="ATC": b_A=1,b_C=1,b_T=1, M=1+1+1=3, ρ=9. Let's directly compute ρ manually:
s = A C T
t = A T C
List shifts:
s0: A C T
s1: C T A
s2: T A C
t0: A T C
t1: T C A
t2: C A T
Now compute h for each pair:
h(s0, t0): A==A?1, C==T?0, T==C?0 ->1
h(s0, t1): A==T?0, C==C?1, T==A?0 ->1
h(s0, t2): A==C?0, C==A?0, T==T?1 ->1
s1 vs t0: C==A?0, T==T?1, A==C?0 ->1
s1 vs t1: C==T?0, T==C?0, A==A?1 ->1
s1 vs t2: C==C?1, T==A?0, A==T?0 ->1
s2 vs t0: T==A?0, A==T?0, C==C?1 ->1
s2 vs t1: T==T?1, A==C?0, C==A?0 ->1
s2 vs t2: T==C?0, A==A?1, C==T?0 ->1
Sum =9. Good.
Now pick a t that includes a non-max letter, say s="ACT", t="ACG". t includes G (non-max). a_G=0, so M = a_Ab_A + a_Cb_C + a_Tb_T + a_Gb_G = 11 + 11 + 10 + 01 =2. So predicted ρ = 3*2 =6. Direct compute:
s=ACT, t=ACG. shifts of t: ACG, CGA, GAC.
Compute h(s0=ACT, t0=ACG): A==A(1), C==C(1), T==G(0) ->2
s0 vs t1=CGA: A==C?0, C==G?0, T==A?0 ->0
s0 vs t2=GAC: A==G?0, C==A?0, T==C?0 ->0
s1=CTA vs t0: C==A?0, T==C?0, A==G?0 ->0
s1 vs t1: C==C?1, T==G?0, A==A?1 ->2
s1 vs t2: C==G?0, T==A?0, A==C?0 ->0
s2=TAC vs t0: T==A?0, A==C?0, C==G?0 ->0
s2 vs t1: T==C?0, A==G?0, C==A?0 ->0
s2 vs t2: T==G?0, A==A?1, C==C?1 ->2
Total=2+0+0+0+2+0+0+0+2=6. Good.
Thus formula holds.
Now, is it always true that ρ = n * (sum over pairs)? Let's test n=2, s="AG", t="AG": we already did, ρ=4, sum over pairs: pairs: (0,0):A==A?1, (0,1):A==G?0, (1,0):G==A?0, (1,1):G==G?1 => sum=2, times n=2 gives 4. Good.
So answer is correct.
Now, but wait: The problem statement says: "Function h(s, t) can be used to define the function of Vasya distance ρ(s, t): ρ(s, t) = Σ_{i=0}^{n-1} Σ_{j=0}^{n-1} h(s_i, t_j) where s_i is obtained from string s, by applying left circular shift i times." So indeed.
Thus solution: count max freq letters, compute pow(cnt, n, MOD).
But given the problem is from some contest (maybe Codeforces Round #...), I recall a problem named "DNA Sequence" where the answer is indeed pow(k, n) with k the number of most frequent nucleotides. However, there might be a twist: The distance is defined differently? Actually there is a known Codeforces problem "Vasya and DNA" (maybe problem 323A?). Let me search memory: There is a problem "Cyclic DNA" but not sure.
Given the examples, they match.
Thus I'll implement.
But before finalizing, let's consider potential pitfalls: Mod exponentiation with large exponent (n up to 1e5) is fine. Need to use fast exponentiation modulo MOD.
Edge Cases: n large, string length 1e5, counts simple.
Also note that the input string consists of characters 'A','C','G','T' only. So we can simply count.
One more thought: Could there be any scenario where the maximum distance is achieved by t that are not necessarily only from max letters but due to some cyclic symmetry that influences something? The derivation shows that ρ depends only on frequencies of characters, not on the order of characters in t. Because ρ = n * Σ_{u,v} I(s[u]=t[v]) = n * Σ_c a_c b_c. That does not depend on positions. So any t with the same multiset of characters yields the same ρ. So the order doesn't matter. So the maximum is solely determined by b_c.
Thus it's purely combinatorial optimization.
Now, is there any possibility that ρ could be larger if we consider that the sum over i,j includes i and j from 1 to n? Already considered.
Thus answer is correct.
But I've seen a problem called "Vasya and DNA" where the answer is (number of letters with max frequency)^n. That's indeed a known problem from Codeforces Round #... I think it's problem 1096B? Not sure.
Actually I recall a problem "Vasya and String" but that's different.
Let's check Codeforces: There is a problem "Vasya and DNA" (maybe Round #1108?). Not sure.
Nevertheless, the solution is straightforward.
Now, I'll produce final answer.
But wait: I want to double-check the note: "Please note that if for two distinct strings t1 and t2 values ρ(s, t1) and ρ(s, t2) are maximum among all possible t, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one." That's fine.
Now, just to be thorough, maybe the maximum could also be achieved by t that are not composed solely of max letters if the maximum frequency is not unique? Actually we said if max frequency is unique, then only t with all characters equal to that max letter achieve max. That yields b_max = n, and b_other=0. So t consists only of that letter. Could there be t with mix that still gives M = A_max * n? Suppose there is a letter d with a_d = A_max - 1. Then to have M = A_maxn, we need sum_{c} a_c b_c = A_maxn. Since sum b_c = n, rewrite M = A_maxn - Σ_{c} (A_max - a_c) b_c. For M to equal A_maxn, we need (A_max - a_c) b_c = 0 for all c. Since A_max - a_c > 0 for c not in C_max, we need b_c = 0 for those. For c in C_max, A_max - a_c = 0, so no restriction. So indeed b_c can be any nonnegative for c in C_max, as long as sum to n. So if there is more than one max letter, any combination works. So if max is unique, only one letter in C_max, then b_{max} must be n, so t must be all that letter.
Thus answer.
Now, we need to output modulo 1e9+7.
Thus algorithm:
- Read n.
- Read string s.
- Initialize counts for 'A','C','G','T' to 0.
- For each char c in s: increment count.
- Find maxCount = max of counts.
- Let cntMax = number of letters (among 4) with count == maxCount.
- Compute answer = pow_mod(cntMax, n, MOD).
- Print answer.
Edge Cases: n=0? Not allowed (n≥1). So fine.
Complexity: O(n).
Now, verify with examples:
Input:
1
C => counts: C=1, others 0, max=1, cntMax=1, 1^1=1.
Input:
2
AG => counts: A=1,G=1, max=1, cntMax=2, 2^2=4.
Input:
3
TTT => counts: T=3, max=3, cntMax=1, 1^3=1.
Matches.
Thus answer.
But wait, there is one more nuance: The problem asks: "how many strings t there are such that the Vasya distance from the string s attains maximum possible value." According to our derivation, maximum ρ is n^2 * maxFreq. However, is it possible that some t yields a larger ρ than n * (A_max * n) due to some other arrangement? Could there be some t where the sum over pairs of matches is larger than a·b? No, because we derived that sum is exactly a·b. So no.
But hold on: The expression we derived for ρ = n * Σ_{u,v} I(s[u]=t[v]) is correct. However, is Σ_{u,v} I(s[u]=t[v]) exactly a·b? Yes, because we can group by character. But is there any chance that some (u,v) pair could be counted multiple times due to cyclic shifts? No, we already accounted.
Thus done.
Now, before finalizing, I want to double-check the derivation again using generating functions: Alternatively, we can view ρ as sum_{i,j,k} δ_{s[i+k], t[j+k]}. Let p = i+k mod n, q = j+k mod n. For each fixed k, (i,j) -> (p,q) is a bijection. So inner sum becomes Σ_{p,q} δ_{s[p], t[q]} = Σ_{c} a_c b_c. Then multiply by n. So yes.
Thus solution is trivial.
But given the problem is from bioinformatics and seems nontrivial, maybe there is a twist: The maximum might be attained by strings t that are not necessarily over the most frequent letters, if we consider that the sum over i and j of h includes also the case where i and j are both shifted, and maybe there's an interplay that yields a different count? But we already accounted.
I recall a known problem "Cyclic Shift" where you need to maximize sum_{i,j} matches between all cyclic shifts, and the answer is to choose t such that its character distribution is the same as s's. But that's different because here we sum over all pairs of shifts, not just one alignment. Actually if we had to maximize h(s, t) alone, we'd pick t = s. But here we sum over all shifts, so distribution matters.
Alternatively, consider if s has a certain multiset, maybe the best t is the one that matches the most frequent character, not necessarily the same multiset.
Thus answer is as derived.
Given the simplicity, it's probably correct.
Now, I'll produce final answer in Python.
But wait, there's also a possibility of overflow: need to use mod.
Now, implement in code.
Potential pitfalls: The input string might contain newline. Use input().strip().
Now, sample test:
Input:
3
TTT
-> answer 1.
Everything fine.
Thus final solution.
But I'll also consider if n can be as large as 10^5, and m can be 1,2,3,4. So pow modular is fine.
Thus final answer.
I'll now produce final explanation and code.
But before concluding, I want to test with random small n and brute force to ensure formula matches for all possible s. I can simulate mentally for n=2 all s:
All possible s of length 2 over 4 letters: there are 16 possibilities. For each s, we can compute a_c, max, m, answer = m^2. And then brute check all 16 t's to see which yield maximum ρ, see if count matches.
But I'm confident.
One more check: s = "AC": a_A=1,a_C=1,a_G=0,a_T=0, max=1,m=2 => answer=4. That matches earlier.
s = "AA": a_A=2, others 0, max=2,m=1 => answer=1.
s = "AT": similar, answer=4.
s = "AG": answer=4.
s = "CT": answer=4? a_C=1,a_T=1, max=1,m=2 => answer=4.
s = "GT": same.
s = "GG": a_G=2,m=1 => 1.
s = "AC"? already.
Now, s = "TA": same as "AT", answer 4.
Thus consistent.
Now, what about s with three distinct letters: e.g., "ACG": a_A=1,a_C=1,a_G=1, max=1,m=3 => answer=9? Actually n=3 => answer=3^3=27. That's 27. That's plausible.
Now, note that the total number of strings t is 4^n. For s with max frequency less than n, the number of t achieving max is m^n, which could be a significant fraction. For m=4, it's all strings, i.e., answer=4^n. But m=4 only when all four letters have same frequency? But n must be divisible by 4? Actually for m=4, we need all four frequencies equal to max, so each frequency = max, and they sum to n, so max = n/4. So n must be multiple of 4. So answer=4^n. That's fine.
Now, consider s = "AAAA": a_A=4, max=4, m=1, answer=1. So only t="AAAA". But is it possible that t="AAAA" yields maximum? Yes. Could any other t yield same? No, because to achieve M = 4*4=16, we need b_A=4. So only one.
Now, s = "AACC": answer=2^4=16. That is 16 out of 256 possible t. Good.
Thus answer.
Now, implement.
One more nuance: The modulo is 1e9+7 = 1000000007.
Use pow(base, exp, mod) in Python.
Now, final code:
def solve():
import sys
MOD = 10**9+7
data = sys.stdin.read().strip().split()
if not data: return
n = int(data[0])
s = data[1]
from collections import Counter
cnt = Counter(s)
max_freq = max(cnt.values())
m = sum(1 for v in cnt.values() if v == max_freq)
# Note: there are exactly 4 letters, but if some letter didn't appear, its count is 0, not in Counter. So we need to consider all 4 letters: if a letter not in counter, its count is 0. For max_freq, if max_freq > 0, then letters with count 0 are not max unless max_freq is 0 which can't happen. So we can just iterate over the known keys. But need to consider the case where max_freq is 0? impossible. So m is number of letters among A,C,G,T that have count == max_freq. Since Counter only contains keys with count > 0, if max_freq > 0, letters not present have count 0 < max_freq, so they don't count. So we can just count among the four predefined letters using counts from Counter (default 0). Safer: define a dict with all four letters.
# Let's do: freq = {'A':0,'C':0,'G':0,'T':0}; then update from s.
# But given constraints, it's fine.
# Actually to be safe, compute frequencies explicitly.
freq = {'A':0, 'C':0, 'G':0, 'T':0}
for ch in s:
freq[ch] += 1
max_freq = max(freq.values())
m = sum(1 for v in freq.values() if v == max_freq)
ans = pow(m, n, MOD)
print(ans)
But wait: what about the case where there are multiple letters with max frequency? Example: s = "ACGT", n=4, frequencies all 1, max=1, m=4. Answer = 4^4 = 256. That's all possible t? Actually total possible t is 4^4=256. So answer = 256, meaning all strings t achieve maximum. Let's test: s="ACGT". For any t, M = a·b = 1b_A + 1b_C + 1b_G + 1b_T = b_A+b_C+b_G+b_T = n = 4. So M=4 always, because sum of b's is 4. So indeed any t yields M=4. So ρ = nM = 16? Actually ρ = nM =4*4=16. But is that the maximum? Could there be a t that yields higher? Since M = a·b, and a vector is all ones, a·b = sum b = n constant. So all t yield same ρ = n^2? Wait n^2 = 16, yes. So all t give same distance. So maximum is attained by all t. So count = 4^4 =256. That matches m^n =4^4=256. Good.
Thus edge case where all letters appear equally (max = n/4) leads to m=4. So that works.
Now, code as above.
But wait: there's a subtlety: what if max frequency is not unique but some letters have frequency equal to max but others have same frequency? Already considered.
Thus final.
But I realize: The derivation gave ρ = n * Σ_{u,v} I(s[u]=t[v]). But we must ensure that this sum is over all ordered pairs (u,v). That's n^2 terms. So ρ = n * (number of matches across all position pairs). This includes the diagonal pairs and cross pairs. It's plausible.
Now, verify for n=2, s="AA", t="AA": Σ_{u,v} I = 4 (since s[0]=A matches t[0] and t[1], similarly s[1] matches both). So Σ=4, n=2 => ρ=8, matches earlier.
For s="AA", t="AB": t[0]=A, t[1]=B. Then Σ = (s[0]=A matches t[0]?1; t[1]?0) + (s[1]=A matches t[0]?1; t[1]?0) = 2. ρ=4, matches earlier.
Thus works.
Now, ensure that the maximum Σ over t is indeed max_freq * n? Since Σ = Σ_c a_c b_c, and Σ b_c = n, so maximum is max_freq * n. So yes.
Thus answer.
Given the simplicity, it's likely correct.
Now, before finalizing, I want to read the problem statement again as given in the text to confirm no misinterpretation:
"Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences.
Let's assume that strings s and t have the same length n, then the function h(s, t) is defined as the number of positions in which the respective symbols of s and t are the same. Function h(s, t) can be used to define the function of Vasya distance ρ(s, t):
where is obtained from string s, by applying left circular shift i times. For example, ρ("AGC", "CGT") = h("AGC", "CGT") + h("AGC", "GTC") + h("AGC", "TCG") + h("GCA", "CGT") + h("GCA", "GTC") + h("GCA", "TCG") + h("CAG", "CGT") + h("CAG", "GTC") + h("CAG", "TCG") = 1 + 1 + 0 + 0 + 1 + 1 + 1 + 0 + 1 = 6
Vasya found a string s of length n on the Internet. Now he wants to count how many strings t there are such that the Vasya distance from the string s attains maximum possible value. Formally speaking, t must satisfy the equation: .
Vasya could not try all possible strings to find an answer, so he needs your help. As the answer may be very large, count the number of such strings modulo 109 + 7."
Thus exactly.
Now, answer as per analysis.
I'll produce final solution.
But before writing final, consider if there is any possibility of misinterpretation: The image might show the sum from i=1 to n, but they wrote i=0 to n-1 in text. In the example they used all shifts: for "AGC" (n=3), they listed shifts: "AGC", "GCA", "CAG". That's three shifts (i=0,1,2). So indeed.
Thus fine.
Now, let's think about time and memory: trivial.
Thus final answer.
Wait: what about integer overflow in intermediate multiplication? Python handles big ints, but we use modulo exponent.
Thus final.
Now, I'll produce final answer. The Vasya distance simplifies to ρ(s, t) = n · Σ_{u,v} [s[u] = t[v]] = n · Σ_c a_c b_c, where a_c is the frequency of character c in s and b_c is its frequency in t.
Maximizing ρ(s, t) is equivalent to maximizing M = Σ_c a_c b_c under the constraints Σ_c b_c = n, b_c ≥ 0.
The maximum is achieved by allocating all n characters of t to the letters that have the maximum frequency in s. If the maximum frequency is A_max and occurs for m distinct letters, then any t composed only of those m letters gives M = A_max·n, which is optimal.
Thus the number of optimal strings t is m^n (each of the n positions can be chosen independently from the m letters).
Compute the frequencies of 'A', 'C', 'G', 'T' in s, find the maximum frequency, count how many letters attain it (m), and output m^n mod (10^9+7).
MOD = 10**9 + 7
def solve() -> None:
import sys
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
s = data[1]
freq = {'A': 0, 'C': 0, 'G': 0, 'T': 0}
for ch in s:
freq[ch] += 1
max_freq = max(freq.values())
m = sum(1 for v in freq.values() if v == max_freq)
ans = pow(m, n, MOD)
print(ans)
if __name__ == "__main__":
solve()