Daniel's having a lot of fun at the ski resort! Halfway through, however, he notices that his best friend Max isn't here. Now, where could he be...
Unbeknownst to all, Max Sun has been captured by a gang of ski cults, who cannot stand the inert heat of the Sun melting all of the snow. Hence, they must disable him.
The ski gang is made up of ski cults following a strict ranking that orders each cult from highest to lowest rank (the -th cult has rank ). They also have access to types of cutting-edge snowballs indexed from to . The -th type of snowballs is only accessible to cults (which could be none!). In particular, denotes the -th cult that has access to the -th type of snowball.
Now, the gang will attack Max times. Each time, all snowball types in the range are activated, and the highest ranking cults (lowest rank #) are allowed to attack Max. Each of these cults, if they have access to one or more types of snowballs, will choose exactly one type of snowball to use, though each type of snowball can be used by multiple cults. If a cult does not have access to any snowballs, they will simply avoid attacking Max at this time.
For each attack, can you tell Max how many possible attack combinations are possible, modulo ? Two attack combinations are considered different if any cult uses a different type of snowball in the two combinations.
Note: this problem has not been tested in Python or PyPy
Constraints
For all batches:
For each snowball , the cults that have access to are all distinct (i.e. if , then ).
Batch 1 [20%]
The sum of all will not exceed .
Batch 2 [80%]
The sum of all will not exceed .
Input Specification
The first line contains , , and , the number of cults, the number of snowball types, and the number of queries.
The next lines each contain an integer followed by integers: the cults with access to the -th type snowball.
The next lines each contain three integers , , and , denoting the current query.
Output Specification
lines, each with one integer, denoting the answer to the -th query. Output your answers in the order of the input.
Sample Input
7 4 5
4 2 3 4 7
2 1 4
3 1 4 5
1 3
2 3 7
1 3 3
1 3 4
4 4 1
1 4 5
Sample Output
4
2
6
1
12
Sample Explanation
Query | Explanation | Total |
---|---|---|
1 | Cults and have access to both snowball types. Cult can only use snowball . | The product is . |
2 | Cult has access to two snowball types. Cults and have access to only one. Note that only the highest ranking cults may throw snowballs. | The product is . |
3 | Cult has access to two, cults and have access to one, and cult has access to three snowball types. |
The final two cases are calculated similarly
Comments