The Assignment
Limits
1s, 512 MB
Bolt is working at a prominent company called Icefire. He has been given a task for evaluation. The task is about substring triplets. A string s of length n is given at first. To build a substring triplet (s1,s2,s3), the following procedures are followed (1-based indexing):
At first, three unique integers b1,b2,b3 (1≤b1,b2,b3≤n) are selected
Then an integer p (1≤p≤n; max(b1,b2)+p≤n+1) is chosen such that the following two conditions are satisfied:
For each i (0≤i<p), sb1+i=sb2+i
If max(b1,b2)+p≤n, sb1+p=sb2+p
Let, e1 be min(n,b1+p). The substring s1 is the substring sb1,sb1+1,…,se1
Similarly, another integer q (1≤q≤n; max(b3,b2)+q≤n+1) is chosen such that the following two conditions are satisfied:
For each i (0≤i<q), sb3+i=sb2+i
If max(b3,b2)+q≤n, sb3+q=sb2+q
Let, e3 be min(n,b3+q). The substring s3 is the substring sb3,sb3+1,…,se3
Finally, the substring s2 is generated. Let, e2 be min(n,b2+max(p,q)). The substring s2 is the substring sb2,sb2+1,…,se2
The score of the substring triplet is p×q.
For example, let a string s of length n=10 is abababcxya and b1=3,b2=1,b3=10. According to the conditions above, the value of p will be 4 and so, e1=min(n,b1+p)=min(10,3+4)=7. So, the substring s1=ababc from abababcxya. Similarly, the value of q will be 1 and so, e3=min(n,b3+q)=min(10,10+1)=10. So, the substring s3=a from abababcxya. Finally, the value of e2 will be min(n,b2+max(p,q))=min(10,1+max(4,1))=5. So, the substring s2=ababa from abababcxya. The score of this substring triplet (ababc,ababa,a) is p×q=4×1=4.
Icefire tells a substring triplet good if the substrings s1,s2,s3 are non-empty and lexicographically s1<s2<s3 satisfies. Bolt has already generated all possible good substring triplets. Now, he needs to calculate the sum of their scores. Can you calculate it for him?
A substring of a string s is a string that consists of some consecutive characters from string s. A string a is lexicographically smaller than a string b if either a is a prefix of b and a=b, or in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input
The first line contains an integer T (1≤T≤105) — the number of test cases.
The second line contains an integer n (3≤n≤5×105) — the length of the string.
The third line contains the string s — which consists of only lowercase English letters.
The sum of n over all test cases will not exceed 5×105.
Output
In each test case, print a single integer in a line, the sum of the scores of the good substring triplets. As the answer can be very large, print it modulo 998244353.
Sample
Input | Output |
---|
2
10
abababcxya
5
zzzax
| 19
2
|
In the second case, the only good substring triplet is: (za,zza,zzz). Here, the value of p and q are 1 and 2 respectively. So, the answer is = (p×q)mod998244353 = (1×2)mod998244353 = 2. |