#P7531. [USACO21OPEN] Routing Schemes P

[USACO21OPEN] Routing Schemes P

Description

Consider a network consisting of NN nodes numbered 1N1\dots N. Each node is designated as a sender, a receiver, or neither. The number of senders SS is equal to the number of receivers (S1S \ge 1).

The connections between nodes in this network can be represented by a set of directed edges of the form iji \to j, meaning that you can route from node ii to node jj. Interestingly, all edges satisfy i<ji<j, except for KK edges that satisfy i>ji>j (0K20 \le K \le 2). There are no self-loops in the network (no edges of the form iii \to i).

A routing scheme is described by SS directed paths from senders to receivers, where no two paths share the same start or end node. In other words, these paths connect distinct senders to distinct receivers. A path from sender ss to receiver rr can be written as a sequence of nodes s=v0v1v2ve=rs=v_0 \to v_1 \to v_2 \to \cdots \to v_e=r, where for all 0i<e0 \le i < e, the directed edge vivi+1v_i \to v_{i+1} exists. A node may appear more than once in the same path.

Compute the number of routing schemes such that every directed edge is used exactly once. Since the answer may be very large, output it modulo 109+710^9+7. The input guarantees that there exists at least one routing scheme that meets the requirements.

Each input contains TT test cases that should be solved independently.

Input Format

The first line of the input contains TT, the number of test cases.

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

The second line of each test case contains a string of length NN. If the ii-th node is a sender, then the ii-th character of the string is S\texttt{S}; if the ii-th node is a receiver, it is R\texttt{R}; if the ii-th node is neither, it is .\texttt{.}. The number of R\texttt{R} characters equals the number of S\texttt{S} characters, and there is at least one S\texttt{S}.

Each of the next NN lines of a test case contains a 01 string of length NN. If there is a directed edge from node ii to node jj, then the jj-th character of the ii-th line is 11, otherwise it is 00. Since there are no self-loops, the main diagonal of the matrix contains only 00. Besides this, there are exactly KK 11's below the main diagonal.

For readability, adjacent test cases are separated by a blank line.

Output Format

For each test case, output the number of routing schemes in which every edge is used exactly once, modulo 109+710^9+7. The input guarantees that for each test case there exists at least one valid routing scheme.

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
4
12
2

5 1
SS.RR
00101
00100
10010
00000
00000

6 2
S....R
001000
000100
010001
000010
001000
000000
3
1
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
2
1
2
6
24

Hint

Explanation of Sample 1

For the first test case, the edges in the network are $1 \to 3, 2 \to 3, 3 \to 4, 3 \to 5, 4 \to 6, 5 \to 6, 6 \to 7, 6 \to 8$.

There are four possible routing schemes:

  • $1 \to 3 \to 4 \to 6 \to 7, 2 \to 3 \to 5 \to 6 \to 8$
  • $1 \to 3 \to 5 \to 6 \to 7, 2 \to 3 \to 4 \to 6 \to 8$
  • $1 \to 3 \to 4 \to 6 \to 8, 2 \to 3 \to 5 \to 6 \to 7$
  • $1 \to 3 \to 5 \to 6 \to 8, 2 \to 3 \to 4 \to 6 \to 7$

For the second test case, the edges in the network are $1 \to 4, 2 \to 4, 3 \to 4, 4 \to 5, 4 \to 6, 4 \to 7, 8 \to 10, 9 \to 10, 10 \to 11, 11 \to 12$.

One possible routing scheme consists of the following paths:

  • 1451 \to 4 \to 5
  • 2472 \to 4 \to 7
  • 3463 \to 4 \to 6
  • 810128 \to 10 \to 12
  • 910119 \to 10 \to 11

Overall, senders 1,2,3{1,2,3} can route to some permutation of receivers 5,6,7{5,6,7}, and senders 8,9{8,9} can route to some permutation of receivers 11,12{11,12}, so the answer is 6×2=126 \times 2 = 12.

Explanation of Sample 2

For the first test case, the edges in the network are 13,15,23,31,341 \to 3, 1 \to 5, 2 \to 3, 3 \to 1, 3 \to 4.

There are three possible routing schemes:

  • 1315,2341 \to 3 \to 1 \to 5, 2 \to 3 \to 4
  • 134,23151 \to 3 \to 4, 2 \to 3 \to 1 \to 5
  • 15,231341 \to 5, 2 \to 3 \to 1 \to 3 \to 4

For the second test case, the edges in the network are $1 \to 3, 2 \to 4, 3 \to 2, 3 \to 6, 4 \to 5, 5 \to 3$.

There is only one possible routing scheme: 13245361 \to 3 \to 2 \to 4 \to 5 \to 3 \to 6.

Constraints

2N1002 \le N \le 100, 1T201 \le T \le 20, N22×104\sum N^2 \le 2 \times 10^4.

Translated by ChatGPT 5