You are given conditions about an unknown sequence .
The sequence must be a permutation of the integers from to .
For each condition , an integer and a non-empty set of positions are given. The condition is satisfied if
.
For each test case, count the number of permutations that satisfy all conditions. Since the answer can be large, output it modulo .
There are no conditions, so every permutation of is valid.
The first two positions must contain and in any order, and the remaining position must contain .
The first condition needs one of positions to contain . The second condition forces positions and to contain and in some order, so position must contain .
The value would have to appear in both subsets and , but these subsets are disjoint.
The first three positions would all need values at most , but only two values are at most .