Under construction

Still being built. Things may change

Routing Schemes

Time limit1sMemory limit512 MB

Summary
Count the ways to cover every edge of a graph with K backward edges by S paths, each running from a sender to a distinct receiver, modulo 1e9+7.
Level

Hard9 of 10

Topics
Graph, Combinatorics, Dynamic programming
Solved
No attempts yet

Problem

A network has NN nodes (2≤N≤1002\le N\le 100) labeled 11 through NN. Each node is a sender, a receiver, or neither. The number of senders SS equals the number of receivers, and S≥1S \ge 1.

The connections are a list of directed edges i→ji \to j, meaning node ii may route to node jj. Every edge satisfies i<ji<j, except exactly KK edges that satisfy i>ji>j (0≤K≤20 \le K \le 2). There are no self-loops (i→ii \to i).

A routing scheme is a set of SS directed paths, each going from a sender to a receiver. No two paths share an endpoint, so the paths connect distinct senders to distinct receivers. A path s=v0→v1→v2→⋯→ve=rs=v_0 \to v_1 \to v_2 \to \cdots \to v_e=r is valid when the edge vi→vi+1v_i \to v_{i+1} exists for every 0≤i<e0 \le i < e. A node may appear more than once in the same path.

Count the routing schemes in which every directed edge is traversed exactly once. The answer may be very large, so print it modulo 109+710^9+7. Each test case has at least one valid routing scheme.

Input

The first line contains TT, the number of test cases (1≤T≤201 \le T \le 20). Solve each test case independently. The sum of N2N^2 over all test cases does not exceed 2⋅1042 \cdot 10^4.

The first line of each test case contains NN and KK. SS is not given in the input.

The second line is a string of length NN. The ii-th character is S if node ii is a sender, R if node ii is a receiver, and . if it is neither. The number of Rs equals the number of Ss, and there is at least one S.

The next NN lines each contain a bit string of length NN. The jj-th bit of the ii-th line is 1 if there is a directed edge from node ii to node jj, and 0 otherwise. The main diagonal is all zeros. Exactly KK ones lie below the main diagonal.

Output

For each test case, print the number of routing schemes in which every edge is traversed exactly once, modulo 109+710^9+7, on its own line.

Examples3

  1. Example 1

    Input
    2
    
    8 0
    SS....RR
    00100000
    00100000
    00011000
    00000100
    00000100
    00000011
    00000000
    00000000
    
    13 0
    SSS.RRRSS.RR.
    0001000000000
    0001000000000
    0001000000000
    0000111000000
    0000000000000
    0000000000000
    0000000000000
    0000000001000
    0000000001000
    0000000000110
    0000000000000
    0000000000000
    0000000000000
    
    Expected output
    4
    12
    
  2. Example 2

    Input
    2
    
    5 1
    SS.RR
    00101
    00100
    10010
    00000
    00000
    
    6 2
    S....R
    001000
    000100
    010001
    000010
    001000
    000000
    
    Expected output
    3
    1
    
  3. Example 3

    Input
    5
    
    3 2
    RS.
    010
    101
    100
    
    4 2
    .R.S
    0100
    0010
    1000
    0100
    
    4 2
    .SR.
    0000
    0011
    0100
    0010
    
    5 2
    .SSRR
    01000
    10101
    01010
    00000
    00000
    
    6 2
    SS..RR
    001010
    000010
    000010
    000010
    100101
    000000
    
    Expected output
    2
    1
    2
    6
    24