#P2375. [NOI2014] 动物园

[NOI2014] 动物园

Description

Recently, the director found that more and more animals in the zoo were lazy and only cared about eating. For example, penguins only act cute to ask visitors for food. To rectify this bad trend and make the animals ask for food with their real skills, the director decided to open an algorithms class so that the animals could learn algorithms.

One day, the director explained the KMP algorithm.

Director: "For a string SS of length LL, we can compute in O(L)O(L) time an array called next\mathrm{next}. Has anyone previewed the meaning of the next\mathrm{next} array?"

Panda: "For the substring formed by the first ii characters of string SS, among the strings that are both its suffix and its prefix (excluding the substring itself), the maximum length is denoted as next[i]\mathrm{next}[i]."

Director: "Very good! Can you give an example?"

Panda: "For example, if SS is abcababc\verb!abcababc!, then next[5]=2\mathrm{next}[5] = 2. This is because the first 55 characters of SS are abcab\verb!abcab!, and ab\verb!ab! is both its suffix and its prefix, and there is no longer string that satisfies this property. Similarly, we can also get $\mathrm{next}[1] = \mathrm{next}[2] = \mathrm{next}[3] = 0$, next[4]=next[6]=1\mathrm{next}[4] = \mathrm{next}[6] = 1, next[7]=2\mathrm{next}[7] = 2, next[8]=3\mathrm{next}[8] = 3."

The director praised Panda for previewing diligently. Then he explained in detail how to compute the next\mathrm{next} array in O(L)O(L) time.

Before class ended, the director posed a question: "The KMP algorithm can only compute the next\mathrm{next} array. I now want to compute a more powerful array num\mathrm{num} — for the substring formed by the first ii characters of string SS, count the number of strings that are both its suffix and its prefix, and moreover the suffix and the prefix do not overlap. Denote this count as num[i]\mathrm{num}[i]. For example, if SS is aaaaa\verb!aaaaa!, then num[4]=2\mathrm{num}[4] = 2. This is because the first 44 characters of SS are aaaa\verb!aaaa!, among which a\verb!a! and aa\verb!aa! both satisfy the property 'being both a suffix and a prefix', while ensuring that the suffix and the prefix do not overlap. Although aaa\verb!aaa! also satisfies 'being both a suffix and a prefix', unfortunately its suffix and prefix overlap, so it cannot be counted. Similarly, $\mathrm{num}[1] = 0, \mathrm{num}[2] = \mathrm{num}[3] = 1, \mathrm{num}[5] = 2$."

Finally, the director announced a reward: the first student to solve it correctly would get a box of chocolates. Hearing this, the penguin who had slept through the class suddenly woke up! But the penguin could not solve the problem, so it asked you, a visitor to the zoo, for help. Can you help the penguin write a program to compute the num\mathrm{num} array?

In particular, to avoid massive output, you do not need to print each num[i]\mathrm{num}[i]. You only need to output the product of all (num[i]+1)(\mathrm{num}[i] + 1), modulo 109+710^9 + 7.

Input Format

The first line contains a single positive integer nn, the number of test cases.
Then follow nn lines, each describing one test case. Each test case contains a single string SS. See the problem statement for the definition of SS. It is guaranteed that SS contains only lowercase letters. The input file will not contain extra blank lines, and there will be no trailing spaces at the end of lines.

Output Format

Output nn lines, each describing the answer for one test case, in the same order as the input. For each test case, output a single integer, which is the answer modulo 109+710^9 + 7. The output file should not contain extra blank lines.

3
aaaaa
ab
abcababc
36
1
32 

Hint

Test point ID Constraints
1 n5,L50n \le 5, L \le 50
2 n5,L200n \le 5, L \le 200
3
4 n5,L10,000n \le 5, L \le 10{,}000
5
6 n5,L100,000n \le 5, L \le 100{,}000
7 n5,L200,000n \le 5, L \le 200{,}000
8 n5,L500,000n \le 5, L \le 500{,}000
9 n5,L1,000,000n \le 5, L \le 1{,}000{,}000
10

Translated by ChatGPT 5