#P1540E. Tasty Dishes
Tasty Dishes
Description
Note that the memory limit is unusual.
There are chefs numbered that must prepare dishes for a king. Chef has skill and initially has a dish of tastiness where . Each chef has a list of other chefs that he is allowed to copy from. To stop chefs from learning bad habits, the king makes sure that chef can only copy from chefs of larger skill.
There are a sequence of days that pass during which the chefs can work on their dish. During each day, there are two stages during which a chef can change the tastiness of their dish.
- At the beginning of each day, each chef can choose to work (or not work) on their own dish, thereby multiplying the tastiness of their dish of their skill () (or doing nothing).
- After all chefs (who wanted) worked on their own dishes, each start observing the other chefs. In particular, for each chef on chef 's list, chef can choose to copy (or not copy) 's dish, thereby adding the tastiness of the 's dish to 's dish () (or doing nothing). It can be assumed that all copying occurs simultaneously. Namely, if chef chooses to copy from chef he will copy the tastiness of chef 's dish at the end of stage .
All chefs work to maximize the tastiness of their own dish in order to please the king.
Finally, you are given queries. Each query is one of two types.
- — find the sum of tastiness after the -th day. Because this value can be large, find it modulo .
- — the king adds tastiness to the -th chef's dish before the -st day begins (). Note that, because the king wants to see tastier dishes, he only adds positive tastiness ().
Note that queries of type are independent of each all other queries. Specifically, each query of type is a scenario and does not change the initial tastiness of any dish for future queries. Note that queries of type are cumulative and only change the initial tastiness of a dish. See notes for an example of queries.
The first line contains a single integer () — the number of chefs.
The second line contains integers ().
The next lines each begin with a integer (), denoting the number of chefs the -th chef can copy from. This number is followed by distinct integers (), signifying that chef is allowed to copy from chef during stage of each day.
The next line contains a single integer () — the number of queries.
Each of the next lines contains a query of one of two types:
- (; );
- (; ).
It is guaranteed that there is at least one query of the first type.
For each query of the first type, print a single integer — the answer to the query.
Input
The first line contains a single integer () — the number of chefs.
The second line contains integers ().
The next lines each begin with a integer (), denoting the number of chefs the -th chef can copy from. This number is followed by distinct integers (), signifying that chef is allowed to copy from chef during stage of each day.
The next line contains a single integer () — the number of queries.
Each of the next lines contains a query of one of two types:
- (; );
- (; ).
It is guaranteed that there is at least one query of the first type.
Output
For each query of the first type, print a single integer — the answer to the query.
Note
Below is the set of chefs that each chef is allowed to copy from:
- :
- :
- :
- :
- : (no other chefs)
Following is a description of the sample.
For the first query of type , the initial tastiness values are .
The final result of the first day is shown below:
- (chef works on his dish).
- (chef and chef copy from chef ).
So, the answer for the -st query is .
For the -th query (-rd of type ). The initial tastiness values are now .
Day 1
- (chefs and work on their dishes).
- (chef copies from chefs and , chef copies from chef , chef copies from chef ).
Day 2
- (all chefs but chef work on their dish).
- (chef copies from chefs , and , chef from , chef from , chef from chef ).
So, the answer for the -th query is .
It can be shown that, in each step we described, all chefs moved optimally.