409658: GYM103660 K Substring Inversion (Hard Version)
Description
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]$$$
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$$$.
InputThe 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.
OutputFor each test case, output a single integer represents the answer module $$$10^9+7$$$.
ExampleInput2 3 aab 3 abaOutput
2 4