Routing Schemes
Time limit1sMemory limit512 MB
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 nodes () labeled through . Each node is a sender, a receiver, or neither. The number of senders equals the number of receivers, and .
The connections are a list of directed edges , meaning node may route to node . Every edge satisfies , except exactly edges that satisfy (). There are no self-loops ().
A routing scheme is a set of 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 is valid when the edge exists for every . 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 . Each test case has at least one valid routing scheme.
Input
The first line contains , the number of test cases (). Solve each test case independently. The sum of over all test cases does not exceed .
The first line of each test case contains and . is not given in the input.
The second line is a string of length . The -th character is S if node is a sender, R if node 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 lines each contain a bit string of length . The -th bit of the -th line is 1 if there is a directed edge from node to node , and 0 otherwise. The main diagonal is all zeros. Exactly 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 , on its own line.