luogu#P4982. 规划

规划

Background

After a long period of hard work, TimeTraveller {\rm TimeTraveller\ } finally got into his dream school.

Problem Description

As a foodie,  TimeTraveller{\rm \ TimeTraveller} did not go to register on his first day after enrollment. Instead, he went to the cafeteria to check the dishes. However, for various reasons, there are very few dishes in the cafeteria this semester, and the cafeteria has made a menu for several days. Therefore, during this semester, the dishes provided each day in the future will cycle in turn according to the menu.

After hearing this, TimeTraveller {\rm TimeTraveller\ } was of course devastated, but he still hopes that what he eats every day will not be too repetitive. So  TimeTraveller {\rm \ TimeTraveller\ } decides that it is enough as long as the dishes he eats do not overlap with the previous day. But as a foodie,  TimeTraveller {\rm \ TimeTraveller\ } also does not want to go hungry, so he must eat at least one dish every day.

TimeTraveller {\rm TimeTraveller\ } wants to know how many legal planning schemes he has, but he finds there are simply too many. So he asks you for help and hopes you can write a program to compute it for him.

Input Format

The first line contains three positive integers n,m,kn,m,k, meaning this semester has nn days, there are mm kinds of dishes in total, and the school has made a kk-day menu (all dishes are numbered from 11 to mm, and it is guaranteed that nkn \ge k).

The next kk lines describe the menu. In each line, the first number is pp, meaning the school prepared pp dishes on that day, followed by pp numbers indicating which dishes they are (it is guaranteed that pp does not exceed mm, and these pp numbers are all distinct).

Output Format

Output the number of legal schemes. Since the answer may be too large, you only need to output the value modulo 1e9+71e9+7.

3 3 2
2 1 3
2 2 3

11
10 7 3
5 1 2 3 4 5
3 1 3 7
4 1 2 6 7

730285459

Hint

Sample 11 Explanation:

Scheme 11: On day 11, eat dishes 1,31,3; on day 22, eat dish 22; on day 33, eat dishes 1,31,3.

Scheme 22: On day 11, eat dishes 1,31,3; on day 22, eat dish 22; on day 33, eat dish 33.

Scheme 33: On day 11, eat dishes 1,31,3; on day 22, eat dish 22; on day 33, eat dish 11.

Scheme 44: On day 11, eat dish 33; on day 22, eat dish 22; on day 33, eat dishes 1,31,3.

Scheme 55: On day 11, eat dish 33; on day 22, eat dish 22; on day 33, eat dish 33.

Scheme 66: On day 11, eat dish 33; on day 22, eat dish 22; on day 33, eat dish 11.

Scheme 77: On day 11, eat dish 11; on day 22, eat dishes 2,32,3; on day 33, eat dish 11.

Scheme 88: On day 11, eat dish 11; on day 22, eat dish 33; on day 33, eat dish 11.

Scheme 99: On day 11, eat dish 11; on day 22, eat dish 22; on day 33, eat dishes 1,31,3.

Scheme 1010: On day 11, eat dish 11; on day 22, eat dish 22; on day 33, eat dish 33.

Scheme 1111: On day 11, eat dish 11; on day 22, eat dish 22; on day 33, eat dish 11.

Constraints:

  • For 20%20\% of the testdata, n5,m7,k5n \le 5, m \le 7, k \le 5.

  • For 45%45\% of the testdata, n50000,m7,k7n \le 50000, m \le 7, k \le 7.

  • For another 10%10\% of the testdata, n107,m2,k=1n \le 10^7, m \le 2, k = 1.

  • For 70%70\% of the testdata, n107,m7,k7n \le 10^7, m \le 7, k \le 7.

  • For 100%100\% of the testdata, n107,m7,k300n \le 10^7, m \le 7, k \le 300.

Translated by ChatGPT 5