The first line of the input contains T, the number of test cases.The first line of each test case contains the integers N and K. Note that S is not explicitly given within the input.
The second line of each test case contains a string of length N. The i-th character of the string is equal to S if the i-th node is a sender, R if the i-th node is a receiver, or . if the i-th node is neither. The number of Rs in this string is equal to the number of S, and there is at least one S.
The next N lines of each test case each contain a bit string of N zeros and ones. The j-th bit of the i-th line is equal to 1 if there exists a directed edge from node i to node j, and 0 otherwise. As there are no self-loops, the main diagonal of the matrix consists solely of zeros. Furthermore, there are exactly K ones below the main diagonal.
Consecutive test cases are separated by newlines for readability.
For each test case, the number of routing schemes such that every edge is traversed exactly once, modulo 109+7. It is guaranteed that there is at least one valid routing scheme for each test case.
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
For the first test case, the edges are 1→3,2→3,3→4,3→5,4→6,5→6,6→7,6→8
There are four possible routing schemes:
1→3→4→6→7,2→3→5→6→81→3→5→6→7,2→3→4→6→81→3→4→6→8,2→3→5→6→71→3→5→6→8,2→3→4→6→7For the second test case, the edges are 1→4,2→4,3→4,4→5,4→6,4→7,8→10,9→10,10→11,10→12.
One possible routing scheme consists of the following paths:
1→4→52→4→73→4→68→10→129→10→11In general, senders {1,2,3} can route to some permutation of receivers {5,6,7} and senders {8,9}can route to some permutation of receivers {11,12}, giving an answer of 6⋅2=12.
2 5 1 SS.RR 00101 00100 10010 00000 00000 6 2 S....R 001000 000100 010001 000010 001000 000000
For the first test case, the edges are 1→3,1→5,2→3,3→1,3→4.
There are three possible routing schemes:
1→3→1→5, 2→3→41→3→4, 2→3→1→51→5, 2→3→1→3→4For the second test case, the edges are 1→3,2→4,3→2,3→6,4→5,5→3.
There is only one possible routing scheme: 1→3→2→4→5→3→6.
Test cases 4-5 satisfy N≤6.Test cases 6-7 satisfy K=0.Test cases 8-12 satisfy K=1.Test cases 13-24 satisfy K=2Problem credits: Benjamin Qi