Welcome to ZOJ
Select Problem
ZOJ Problem Set - 3874
Permutation Graph

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Edward has a permutation {a1, a2, … an}. He finds that if he connects each pair (ai, aj) such that i < j and ai > aj, he will get a graph.

For example, if the permutation is {2, 3, 1, 4}, then 1 and 2 are connected and 1 and 3 are connected.

Edward lost his permutation, but he does know the connected components of the corresponding graph. He wants to know how many permutations will result in the same connected components.

Note that two vertices u, v belong to the same connected component if there exists a sequence of vertices starting with u and ending with v such that every two subsequent vertices in the sequence are connected by an edge.


There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers n, m (1 ≤ mn ≤ 100000), indicating the length of the permutation and the number of connected components in the graph.

Each of the following m lines contains an integer ci which denotes the size of i-th connected component, followed by ci distinct integers vi,1, vi,2, … vi,ci which denotes the connected component (1 ≤ ci, vi,1, vi,2, … vi,cin).

It is guaranteed that every number will appear in exact one connected component and c1 + c2 + … + cm = n.


For each case, output the answer modulo 786433.

Sample Input

4 4
1 1
1 2
1 3
1 4
4 2
3 1 2 3
1 4

Sample Output



For the second case, the three permutations is: {2, 3, 1, 4}, {3, 2, 1, 4}, {3, 1, 2, 4}.

Author: LIN, Xi
Source: The 12th Zhejiang Provincial Collegiate Programming Contest
Submit    Status