For each enciphered string, count the sentences of vocabulary words whose letter multisets concatenate to it, modulo 1e9+7.
Medium5Dynamic programmingHash mapCombinatoricsNo attempts yetTime limit5sMemory limit512 MBCodejamon monsters talk in enciphered strings. Each kind of monster has its own vocabulary, a list of V different words made of lowercase English letters.
When a monster speaks, it first builds a sentence out of words from its vocabulary. The same word may appear several times in one sentence. It then turns the sentence into an enciphered string in two steps.
Given an enciphered string, count how many original sentences could have produced it. Two sentences differ when their sequences of words differ, and two vocabulary words that become equal after shuffling still count as different words. For example, with the vocabulary ["this", "is", "a", "dog", "god"], the enciphered string ishtsiaogd comes from four sentences.
is this a dogis this a godthis is a dogthis is a godYou have S enciphered strings from the same monster. For each one, report the number of possible original sentences. The answer can be very large, so print it modulo the prime 109+7.
The first line contains the number of test cases T.
The first line of each test case contains two integers V and S, the size of the vocabulary and the number of enciphered strings. The next V lines each contain one word of the vocabulary. Every word consists of lowercase English letters, and the words are different from each other. The next S lines each contain one enciphered string, also made of lowercase English letters.
Every enciphered string is valid: at least one original sentence produces it.
For each test case, print one line in the form Case #x: y, where x is the test case number starting from 1 and y is the list of the S answers separated by single spaces, in the order the strings were given. Each answer is taken modulo 109+7.