Under construction

Still being built. Things may change

Welcome to ICPCCamp 2017

Time limit1sMemory limit512 MB

Summary
Count the distinct team sets that the (X, Y)-rule can produce from n contest scoreboards and the final ranking, modulo 1e9+7.
Level

Hard8 of 10

Topics
Combinatorics, Greedy, Array
Solved
No attempts yet

Problem

ICPCCamp teams are often selected by a mysterious (X,Y)(X, Y)-rule described in a blog (?).

There are (n+1)(n + 1) selection contests held to choose ICPCCamp team among mm teams labeled 1,2,…,m1, 2, \dots, m. The number of teams attending the ii-th contest is kik_i. The last contest, the (n+1)(n + 1)-th, is EasyCamp-Final, which is very important, so kn+1=mk_{n + 1} = m always holds. The scoreboard of the ii-th contest is ri,1,ri,2,…,ri,kir_{i, 1}, r_{i, 2}, \dots, r_{i, k_i}, which means team ri,jr_{i, j} has rank jj in that contest.

The (X,Y)(X, Y)-rule works as follows. First, two non-negative integers XX and YY and a permutation P={p1,p2,…,pn}P = \{p_1, p_2, \dots, p_n\} of {1,2,…,n}\{1, 2, \dots, n\} are chosen. Then the first X+YX+Y distinct teams in the list {rn+1,1,…,rn+1,Y,rp1,1,rp2,1,…,rpn,1,rp1,2,rp2,2,…,rpn,2,… }\{r_{n + 1, 1}, \dots, r_{n + 1, Y}, r_{p_1, 1}, r_{p_2, 1}, \dots, r_{p_n, 1}, r_{p_1, 2}, r_{p_2, 2}, \dots, r_{p_n, 2}, \dots\} are selected as the ICPCCamp team. In other words, the list starts with the first YY EasyCamp-Final teams, then the top teams of the first nn contests in the order given by PP, then the second teams of the first nn contests in the same order, and so on.

Bobo would like to know the number of possible sets of ICPCCamp team modulo 109+710^9+7, if he can choose XX, YY, and PP arbitrarily.

Wish you enjoy yourself in the upcoming World Finals!

Input

The input contains zero or more test cases and ends at end-of-file. Each test case is given as follows.

The first line contains two integers nn and mm (0≤n≤2⋅1050 \leq n \leq 2 \cdot 10^5, 1≤m≤2⋅1051 \leq m \leq 2 \cdot 10^5).

The ii-th of the following nn lines contains an integer kik_i followed by kik_i integers ri,1,ri,2,…,ri,kir_{i, 1}, r_{i, 2}, \dots, r_{i, k_i} (1≤ki≤m1 \leq k_i \leq m).

The last line contains mm integers rn+1,1,rn+1,2,…,rn+1,mr_{n + 1, 1}, r_{n + 1, 2}, \dots, r_{n + 1, m} (1≤ri,j≤m1 \leq r_{i, j} \leq m). For each ii, the numbers ri,1,…,ri,kir_{i, 1}, \dots, r_{i, k_i} are distinct.

The sum of kik_i and the sum of mm over all test cases do not exceed 2⋅1052 \cdot 10^5.

Output

For each test case, output one integer, the number of possible sets modulo 109+710^9+7.

Examples1

  1. Example 1

    Input
    2 3
    2 1 3
    3 2 1 3
    2 1 3
    0 3
    1 2 3
    
    Expected output
    5
    4