Under construction

Still being built. Things may change

Generator

Time limit3sMemory limit256 MB

Summary
Find the expected number of random draws until all n given length-L sequences have each appeared as a contiguous block of the stream.
Level

Hard9 of 10

Topics
Probability, String matching, Bit manipulation, Dynamic programming
Solved
No attempts yet

Problem

You are given nn different integer sequences. All sequences have the same length LL, and all integers in these sequences are from 11 to mm.

You also have a machine that generates a stream of random numbers. Initially, the stream is empty. Every second, the machine generates a random integer from 11 to mm, inclusive, and appends it to the stream. Each random integer is generated independently with the same probability distribution, which is given to you.

With probability 11, eventually, each of the nn given sequences appears as LL consecutive elements of the stream. The occurrences of different sequences may overlap. At the first moment when each of the nn given sequences has appeared at least once, the machine stops immediately. Your task is to calculate the expected number of seconds after which the machine stops.

Input

There are one or more test cases. The first line of input contains an integer TT, the number of test cases.

Each test case starts with a line containing three positive integers nn, mm and LL: the number of sequences, the upper bound for the integers that may appear in the sequences and in the stream, and the length of each sequence, respectively (1≤n≤151 \le n \le 15, 1≤m≤1001 \le m \le 100, 1≤L≤5⋅1041 \le L \le 5 \cdot 10^4).

The next line contains mm positive integers a1,a2,…,ama_1, a_2, \ldots, a_m describing the probability distribution the machine uses. The machine generates 11 with probability a1/sa_1 / s, 22 with probability a2/sa_2 / s, and so on, where s=a1+a2+…+ams = a_1 + a_2 + \ldots + a_m. It is guaranteed that s≤109s \le 10^9.

Each of the next nn lines contains an integer sequence of length LL. All integers in these sequences are positive and do not exceed mm. It is guaranteed that all nn given sequences are pairwise distinct.

The total sum of n⋅Ln \cdot L over all test cases does not exceed 777 777777\,777.

Output

For each test case, calculate the answer as an irreducible fraction AB\frac{A}{B} and output the integer (A⋅B−1) mod (109+7)(A \cdot B^{-1}) \bmod (10^{9} + 7) on a separate line. Here, B−1B^{-1} is the multiplicative inverse of BB modulo 109+710^{9} + 7.

It is guaranteed that for all given inputs, the integers BB and 109+710^{9} + 7 are relatively prime.

Examples1

  1. Example 1

    Input
    1
    1 2 2
    1 1
    1 1
    
    Expected output
    6