Routing Schemes
Consider a network of nodes labeled . Each node is designated as a sender, a receiver, or neither. The number of senders is equal to the number of receivers .
The connections between the nodes in this network can be described by a list of directed edges each of the form , meaning that node may route to node . Interestingly, all of these edges satisfy the property that , aside from that satisfy . There are no self-loops (edges of the form ).
The description of a "routing scheme" consists of a set of 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 to a receiver can be described as a sequence of nodes
such that the directed edges exist for all . 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 . It is guaranteed that there is at least one routing scheme satisfying these constraints.
Each input contains test cases. It is guaranteed that the sum of over all test cases does not exceed .
Input
The first line contains the number of test cases .
The first line of each test case contains the integers and . Note that is not explicitly given within the input.
The second line of each test case contains a string of length . The -th character of the string is equal to if the -th node is a sender, if the -th node is a receiver, or if the -th node is neither. The number of s in this string is equal to the number of s, and there is at least one .
The next lines of each test case each contain a bit string of zeros and ones. The -th bit of the -th line is equal to if there exists a directed edge from node to node , and otherwise. As there are no self-loops, the main diagonal of the matrix consists solely of zeros. Furthermore, there are exactly 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 . It is guaranteed that there is at least one valid routing scheme for each test case.
Examples
Example 1. For the first test case, the edges are:
There are four possible routing schemes:
For the second test case, the edges are:
One possible routing scheme consists of the following paths:
In general, senders can route to some permutation of receivers and senders can route to some permutation of receivers , giving an answer of .
Example 2. For the first test case, the edges are:
There are three possible routing schemes:
For the second test case, the edges are:
There is only one possible routing scheme: