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): $\rho(s, t) = \sum_{i = 0}^{n - 1} \sum_{j = 0}^{n - 1} h(\operatorname{shift}(s, i), \operatorname{shift}(t, j))$ where $\operatorname{shift}(s, i)$ 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: $\rho(s, t) = \operatorname{max}_{u :|u|=|s|} \rho(s, u)$.

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 10^9 + 7.


-----Input-----

The first line of the input contains a single integer n (1 ≤ n ≤ 10^5).

The second line of the input contains a single string of length n, consisting of characters "ACGT".


-----Output-----

Print a single number — the answer modulo 10^9 + 7.


-----Examples-----
Input
1
C

Output
1

Input
2
AG

Output
4

Input
3
TTT

Output
1



-----Note-----

Please note that if for two distinct strings t_1 and t_2 values ρ(s, t_1) и ρ(s, t_2) 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.

In the first sample, there is ρ("C", "C") = 1, for the remaining strings t of length 1 the value of ρ(s, t) is 0.

In the second sample, ρ("AG", "AG") = ρ("AG", "GA") = ρ("AG", "AA") = ρ("AG", "GG") = 4.

In the third sample, ρ("TTT", "TTT") = 27

Difficulty: MEDIUM
Tags: String algorithms

Mathematics

Starter code:

Solutions:
(n, s) = (int(input()), input())
c = sorted((s.count(b) for b in 'ACGT'))
print(c.count(c[-1]) ** n % (10 ** 9 + 7))


n = int(input())
s = str(input()).upper()
probability_max = 0
Max = 0
modulo = 1000000007
base_acids = [0] * 4
for i in range(n):
	if s[i] == 'A':
		base_acids[0] += 1
	if s[i] == 'C':
		base_acids[1] += 1
	if s[i] == 'G':
		base_acids[2] += 1
	if s[i] == 'T':
		base_acids[3] += 1
kol = base_acids.count(max(base_acids))
print(kol ** n % modulo)


n = int(input().rstrip())
s = str(input().rstrip())
kol = 0
mx = 0
mas = [0] * 4
for i in range(n):
	if s[i] == 'A':
		mas[0] += 1
	if s[i] == 'C':
		mas[1] += 1
	if s[i] == 'G':
		mas[2] += 1
	if s[i] == 'T':
		mas[3] += 1
mx = max(mas)
kol = mas.count(mx)
print(kol ** n % 1000000007)


n = int(input())
s = input()
MOD = 1000000007
A = s.count('A')
C = s.count('C')
G = s.count('G')
T = s.count('T')
m = max(A, C, G, T)
count = (A == m) + (C == m) + (G == m) + (T == m)
print(count ** n % MOD)


import sys
R = lambda : map(int, input().split())
n = int(input())
s = input()
ca = s.count('A')
cg = s.count('G')
cc = s.count('C')
ct = s.count('T')
mx = max(ca, cg, cc, ct)
sum = (ca == mx) + (cg == mx) + (cc == mx) + (ct == mx)
ans = 1
for i in range(n):
	ans = ans * sum % 1000000007
print(ans)


n = int(input())
s = input()
lst = {'A': 0, 'C': 0, 'G': 0, 'T': 0}
(k, l) = (0, 1)
for i in s:
	lst[i] += 1
	if k < lst[i]:
		k = lst[i]
		l = 1
	elif k == lst[i]:
		l += 1
print(l ** n % (10 ** 9 + 7))


from collections import Counter
n = int(input())
ct = list(Counter(input()).values())
print(pow(ct.count(max(ct)), n, int(1000000000.0) + 7))


n = int(input())
s = input().strip()
a = s.count('T')
b = s.count('G')
c = s.count('C')
d = s.count('A')
q = max(a, b, c, d)
e = 0
if q == a:
	e += 1
if q == b:
	e += 1
if q == c:
	e += 1
if q == d:
	e += 1
print(e ** n % 1000000007)


(n, s) = (input(), input())
a = {'A': 0, 'C': 1, 'G': 2, 'T': 3}
A = [0, 0, 0, 0]
for x in s:
	A[a[x]] += 1
MOD = 10 ** 9 + 7
print(A.count(max(A)) ** len(s) % MOD)


n = int(input())
s = input()
dflt = 'ACGT'
a = []
a.append(s.count(dflt[0]))
a.append(s.count(dflt[1]))
a.append(s.count(dflt[2]))
a.append(s.count(dflt[3]))
k = 1
max = a[0]
for i in range(1, 4):
	if a[i] > max:
		k = 1
		max = a[i]
	elif a[i] == max:
		k += 1
print(k ** n % (10 ** 9 + 7))


(_, dna) = (input(), input())
l = sorted([-dna.count(x) for x in set(dna)])
print(l.count(l[0]) ** len(dna) % (10 ** 9 + 7))


n = int(input())
s = input()
MOD = 1000000007
A = C = G = T = 0
for c in s:
	A += c == 'A'
	C += c == 'C'
	G += c == 'G'
	T += c == 'T'
m = max(A, C, G, T)
count = (A == m) + (C == m) + (G == m) + (T == m)
print(count ** n % MOD)


N = int(input())
dna = input()
A_num = 0
C_num = 0
G_num = 0
T_num = 0
for i in range(N):
	if dna[i] == 'A':
		A_num += 1
	elif dna[i] == 'C':
		C_num += 1
	elif dna[i] == 'T':
		T_num += 1
	else:
		G_num += 1
numbers = [A_num, C_num, G_num, T_num]
numbers.sort(key=lambda x: -x)
if numbers[0] > numbers[1]:
	print(1)
elif numbers[1] > numbers[2]:
	print(pow(2, N, 1000000007))
elif numbers[2] > numbers[3]:
	print(pow(3, N, 1000000007))
else:
	print(pow(4, N, 1000000007))


a = int(input())
b = input().lower()
ka = b.count('a')
kb = b.count('c')
kt = b.count('t')
kg = b.count('g')
ans = 0
if ka >= kb and ka >= kt and (ka >= kg):
	km = ka
elif kb >= ka and kb >= kt and (kb >= kg):
	km = kb
elif kt >= ka and kt >= kb and (kt >= kg):
	km = kt
else:
	km = kg
if ka == km:
	ans += 1
if kb == km:
	ans += 1
if kt == km:
	ans += 1
if kg == km:
	ans += 1
if ans == 1 or a == 1:
	print('1')
else:
	print(str(ans ** a % (10 ** 9 + 7)))


def solve(s):
	count = {'A': 0, 'C': 0, 'G': 0, 'T': 0}
	for i in s:
		count[i] += 1
	count_list = []
	for (key, value) in count.items():
		count_list.append(value)
	count_list.sort()
	max_count = count_list[3]
	total = 0
	for i in count_list:
		if i == max_count:
			total += 1
	total_count = 1
	for i in s:
		total_count = total_count * total % 1000000007
	return total_count
n = int(input())
s = input()
result = solve(s)
print(result)


(length, dna) = (int(input()), input())
nums = [dna.count('A'), dna.count('C'), dna.count('G'), dna.count('T')]
print(nums.count(max(nums)) ** length % 1000000007)


n = int(input())
s = input()
from collections import defaultdict
count = defaultdict(int)
for c in s:
	count[c] += 1
max_count = max(count.values())
k = sum([1 for value in list(count.values()) if value == max_count])
mod = 10 ** 9 + 7
answer = 1
for i in range(n):
	answer = answer * k % mod
print(answer)


mod_by = 10 ** 9 + 7
n = int(input())
s = input()
a = s.count('A')
c = s.count('C')
g = s.count('G')
t = s.count('T')
m = max(a, c, g, t)
ct = [a, c, g, t].count(m)
print(pow(ct, n, mod_by))


import itertools
n = int(input())
p = 10 ** 9 + 7
s = input().rstrip()
D = {}
for c in s:
	if c in D:
		D[c] += 1
	else:
		D[c] = 1
vmax = max(D.values())
cmax = {c for c in D if D[c] == vmax}
print(pow(len(cmax), n, p))


modulo = 1000000007
n = int(input())
char_count = [0] * 256
s = input()
for i in range(n):
	char_count[ord(s[i])] += 1
max_char_count = max(char_count)
num_max_char = 0
for i in range(256):
	if char_count[i] == max_char_count:
		num_max_char += 1
print(pow(num_max_char, n) % modulo)


input()
s = input()
k = [s.count(x) for x in 'ACGT']
print(pow(k.count(max(k)), len(s), 10 ** 9 + 7))


n = int(input())
code = str(input()).upper()
cont = [0] * 4
cont[0] = code.count('A')
cont[1] = code.count('C')
cont[2] = code.count('G')
cont[3] = code.count('T')
modulo = 1000000007
res = cont.count(max(cont))
print(res ** n % modulo)


def fast_pow(x, y):
	if y == 0:
		return 1
	p = fast_pow(x, y // 2)
	p = p * p % 1000000007
	if y % 2:
		p = p * x % 1000000007
	return p
n = int(input())
s = input()
cnts = [s.count(ch) for ch in ['A', 'G', 'T', 'C']]
k = cnts.count(max(cnts))
if k == 1:
	print(1)
else:
	print(fast_pow(k, n))


n = int(input())
s = input()
l = [s.count('A'), s.count('C'), s.count('G'), s.count('T')]
print(l.count(max(l)) ** n % 1000000007)


n = int(input())
s = input()
x = [s.count(i) for i in ['A', 'C', 'G', 'T']]
z = x.count(max(x))
print(z ** n % (10 ** 9 + 7))


n = int(input())
s = input()
a = s.count('A')
c = s.count('C')
g = s.count('G')
t = s.count('T')
q = max(a, c, g, t)
cnt = [a, c, g, t].count(q)
print(pow(cnt, n, 1000000007))


n = int(input())
s = input()
ca = cb = cc = cd = 0
for c in s:
	ca += c == 'A'
	cb += c == 'C'
	cc += c == 'G'
	cd += c == 'T'
m = max(ca, cb, cc, cd)
count = (ca == m) + (cb == m) + (cc == m) + (cd == m)
print(count ** n % int(1000000000.0 + 7))


x = int(input())
y = input()
(a, b, c, d) = (y.count('A'), y.count('C'), y.count('G'), y.count('T'))
z = [a, b, c, d]
z.sort()
z.reverse()
same = 1
if z[0] == z[1]:
	same = 2
	if z[1] == z[2]:
		same = 3
		if z[2] == z[3]:
			same = 4
print(pow(same, x, 10 ** 9 + 7))


def main():
	n = int(input())
	l = [0] * 85
	for c in input():
		l[ord(c)] += 1
	(a, c, g, t) = sorted((l[_] for _ in (65, 67, 71, 84)))
	if g < t:
		print(1)
	elif c < g:
		print(pow(2, n, 1000000007))
	elif a < c:
		print(pow(3, n, 1000000007))
	else:
		print(pow(4, n, 1000000007))
main()


numero = int(input())
letras = input()
lista = list(letras)
a = lista.count('A')
t = lista.count('T')
g = lista.count('G')
c = lista.count('C')
numeroSig = max(a, t, g, c)
listaACGT = [a, t, g, c]
z = 0
for i in listaACGT:
	if i == numeroSig:
		z += 1
print(pow(z, numero, 1000000007))


from collections import Counter
n = int(input())
c = Counter(input())
commons = c.most_common()
max_cnt = 0
for x in commons:
	if commons[0][1] == x[1]:
		max_cnt += 1
ans = 1
for i in range(n):
	ans *= max_cnt
	ans = ans % 1000000007
print(ans)


from collections import Counter
n = int(input())
s = input()
mod = int(1000000000.0) + 7
ct = list(Counter(s).values())
t = ct.count(max(ct))
ans = 1
for i in range(n):
	ans = ans * t % mod
print(ans)


n = int(input())
s = input()
num = s.count('A')
num1 = s.count('C')
num2 = s.count('T')
num3 = s.count('G')
num4 = max(num, num1, num2, num3)
num5 = 0
if num == num4:
	num5 += 1
if num1 == num4:
	num5 += 1
if num2 == num4:
	num5 += 1
if num3 == num4:
	num5 += 1
if num5 == 1 or n == 1:
	print(1)
else:
	print(num5 ** n % (10 ** 9 + 7))


n = int(input())
dic = {'A': 0, 'T': 0, 'C': 0, 'G': 0}
st = input()
for i in range(n):
	dic[st[i]] = dic[st[i]] + 1
max = -1
cnt = 0
for s in dic.values():
	if s > max:
		max = s
		cnt = 1
	elif s == max:
		cnt = cnt + 1
ans = 1
for i in range(n):
	ans = ans * cnt % 1000000007
print(ans)


n = int(input())
s = input()
a = [0] * 300
for i in s:
	a[ord(i)] += 1
print(a.count(max(a)) ** len(s) % (10 ** 9 + 7))