#P7801. Equal Sentences
Equal Sentences
Equal Sentences
Problem Description
Sometimes, changing the order of the words in a sentence doesn't influence understanding. For example, if we change "what time is it", into "what time it is"; or change "orz zhang three ak world final", into "zhang orz three world ak final", the meaning of the whole sentence doesn't change a lot, and most people can also understand the changed sentences well. Formally, we define a sentence as a sequence of words. Two sentences and are almost-equal if the two conditions holds:
- The multiset of the words in is the same as the multiset of the words in .
- For a word , its occurrence in and its occurrence in have indexes differing no more than . (The word in the sentence has index .) This holds for all and , as long as the word appears at least times in both sentences. Please notice that "almost-equal" is not a equivalence relation, unlike its name. That is, if sentences and are almost-equal, and are almost-equal, it is possible that and are not almost-equal. Zhang3 has a sentence consisting of words. She wants to know how many different sentences there are, which are almost-equal to , including itself. Two sentences are considered different, if and only if there is a number such that the word in the two sentences are different. As the answer can be very large, please help her calculate the answer modulo .
Input
The first line of the input gives the number of test cases, . test cases follow. For each test case, the first line contains an integer , the number of words in the sentence. The second line contains the sentence consisting of words separated by spaces. Each word consists of no more than lowercase English letters. The sum of in all test cases doesn't exceed .
Output
For each test case, print a line with an integer, representing the answer, modulo .
Sample Input
2
6
he he zhou is watching you
13
yi yi si wu yi si yi jiu yi jiu ba yao ling
Sample Output
8
233
Source
2020 Multi-University Training Contest 4