Digital Circuit
문제
There is a circuit, which consists of gates numbered from to . Gates to are threshold gates, whereas gates to are source gates.
Each gate, except for gate , is an input to exactly one threshold gate. Specifically, for each such that , gate is an input to gate , where . Importantly, we also have . Moreover, we assume . Each threshold gate has one or more inputs. Source gates do not have any inputs.
Each gate has a state which is either or . The initial states of the source gates are given by an array of integers. That is, for each such that , the initial state of the source gate is .
The state of each threshold gate depends on the states of its inputs and is determined as follows. First, each threshold gate is assigned a threshold parameter. The parameter assigned to a threshold gate with inputs must be an integer between and (inclusive). Then, the state of a threshold gate with parameter is , if at least of its inputs have state , and otherwise.
For example, suppose there are threshold gates and source gates. The inputs to gate are gates and , the inputs to gate are gates , , and , and the only input to gate is gate .
This example is illustrated in the following picture.
Suppose that source gates and have state , while source gates and have state . Assume we assign parameters , and to threshold gates , and respectively. In this case, gate has state , gate has state and gate has state . This assignment of parameter values and the states is illustrated in the following picture. Gates whose state is are marked in black.
The states of the source gates will undergo updates. Each update is described by two integers and () and toggles the states of all source gates numbered between and , inclusive. That is, for each such that , source gate changes its state to , if its state is , or to , if its state is . The new state of each toggled gate remains unchanged until it is possibly toggled by one of the later updates.
Your goal is to count, after each update, how many different assignments of parameters to threshold gates result in gate having state . Two assignments are considered different if there exists at least one threshold gate that has a different value of its parameter in both assignments. As the number of ways can be large, you should compute it modulo .
Note that in the example above, there are different assignments of parameters to threshold gates, since gates , and have , and inputs respectively. In out of these assignments, gate has state .