eolymp
bolt
Try our new interface for solving problems
Problems

Routing Schemes

Routing Schemes

Consider a network of n nodes labeled 1..n. Each node is designated as a sender, a receiver, or neither. The number of senders S is equal to the number of receivers (S1).

The connections between the nodes in this network can be described by a list of directed edges each of the form ij, meaning that node i may route to node j. Interestingly, all of these edges satisfy the property that i < j, aside from k that satisfy i > j (0k2). There are no self-loops (edges of the form ii).

The description of a "routing scheme" consists of a set of S directed paths from senders to receivers such that no two of these paths share an endpoint. That is, the paths connect distinct senders to distinct receivers. A path from a sender s to a receiver r can be described as a sequence of nodes

s = v0v1v2 → ... → ve = r

such that the directed edges vivi+1 exist for all 0≤i<e. A node may appear more than once within the same path.

Count the number of distinct routing schemes such that every directed edge is traversed exactly once. Since the answer may be very large, report it modulo 109 + 7. It is guaranteed that there is at least one routing scheme satisfying these constraints.

Each input contains t test cases. It is guaranteed that the sum of n2 over all test cases does not exceed 2 * 104.

Input

The first line contains the number of test cases t (1t20).

The first line of each test case contains the integers n (2n100) 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 Ss, 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.

Output

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.

Example 1

For the first test case, the edges are 13, 23, 34, 35, 46, 56, 67, 68.

There are four possible routing schemes:

1 → 3 → 4 → 6 → 7, 2 → 3 → 5 → 6 → 8
1 → 3 → 5 → 6 → 7, 2 → 3 → 4 → 6 → 8
1 → 3 → 4 → 6 → 8, 2 → 3 → 5 → 6 → 7
1 → 3 → 5 → 6 → 8, 2 → 3 → 4 → 6 → 7

For the second test case, the edges are 14, 24, 34, 45, 46, 47, 810, 910, 1011, 1012.

One possible routing scheme consists of the following paths:

1 → 4 → 5
2 → 4 → 7
3 → 4 → 6
8 → 10 → 12
9 → 10 → 11

In 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.

Example 2

For the first test case, the edges are 13, 15, 23, 31, 34.

There are three possible routing schemes:

1 → 3 → 1 → 5, 2 → 3 → 4
1 → 3 → 4, 2 → 3 → 1 → 5
1 → 5, 2 → 3 → 1 → 3 → 4

For the second test case, the edges are 13, 24, 32, 36, 45, 53.

There is only one possible routing scheme: 1324536.

Time limit 1 second
Memory limit 128 MiB
Input example #1
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
Output example #1
4
12
Input example #2
2

5 1
SS.RR
00101
00100
10010
00000
00000

6 2
S....R
001000
000100
010001
000010
001000
000000
Output example #2
3
1
Input example #3
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
Output example #3
2
1
2
6
24
Source 2021 USACO US Open, Platinum