409658: GYM103660 K Substring Inversion (Hard Version)

Memory Limit:512 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

K. Substring Inversion (Hard Version)time limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

This is the hard version of the problem. The only difference between the two versions is the constraint on $$$n$$$.

Given a string $$$s$$$ of length $$$n$$$ consisting of lowercase letters only, please calculate the number of 4-tuples $$$(a,b,c,d)$$$ satisfying following conditions:

  • $$$1\le a\le b\le n$$$
  • $$$1\le c\le d\le n$$$
  • $$$a < c$$$
  • $$$s[a:b]$$$ is lexicographically greater than $$$s[c:d]$$$
Here, $$$s[i:j]$$$ represents a substring of $$$s$$$ which start from the $$$i$$$-th character and end at $$$j$$$-th character(inclusive).

String $$$x$$$ is lexicographically less than string $$$y$$$, if either $$$x$$$ is a prefix of $$$y$$$ (and $$$x \ne y$$$), or there exists such $$$i(1 \le i \le min(|x|, |y|))$$$, that $$$x_i < y_i$$$, and for any $$$j(1\le j<i), x_j = y_j$$$. Here $$$|a|$$$ denotes the length of the string $$$a$$$.

As the answer may be too big, please output the answer module $$$10^9+7$$$.

Input

The first line contains a single integer $$$T(1\le T \le 10)$$$ — the number of test cases.

The first line of each test case contains a single integer $$$n(1\le n \le 2 \times 10^5)$$$ — the length of the string.

The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of lowercase letters only.

It is guaranteed that $$$\sum{n} \le 2 \times 10^5$$$ over all test cases.

Output

For each test case, output a single integer represents the answer module $$$10^9+7$$$.

ExampleInput
2
3
aab
3
aba
Output
2
4

加入题单

上一题 下一题 算法标签: