We define the following two operations which can be performed on a stack:
- — the number is added to the top of the stack
- — the number on top of the stack is removed from the stack.
A sequence of operations is considered to be correct if, when the operations are performed on an initially empty stack in order, the following two conditions are met:
- No pop operation is performed on an empty stack,
- After the last operation, the stack is empty.
For example, is correct, whereas , or are not.
We will consider a list of operations, numbered from to . By , where , we denote the sequence of operations .
We define the value as follows. If is not a correct sequence, then we define . Otherwise, we perform the operations in order on an initially empty stack. After each operation, we calculate the maximum value in the stack. Let be the maximum value after operation , or zero if the stack is empty. Then, .
Task
You are given , the list of operations, a number , and queries of the form , where . You are also given a number . Depending on the value of , you must calculate the following for all queries:
- If , then you should calculate modulo . It is guaranteed that is correct for all queries.
- If , then you should calculate the sum of for all modulo . It is guaranteed that for every query, if you perform the operations of in order, then no pop operation is performed on an empty stack.
Input data
The first line of input contains the value . The second line contains the integers and . The third line contains non-negative integers which encode as follows:
- If , then
- If , then
Each of the following lines contains two integers and , representing the queries.\
Output data
Each of the lines of output should contain the answers to the queries, in order. All answers must be given modulo .
Restrictions
- , for all
- is guaranteed to be a correct sequence of operations
- We call a sequence of operations finally empty if, when performing these operations on an empty stack, the stack is empty only before the first operation and after the last one. For example, is finally empty, but is not.
# | Points | Restrictions |
---|---|---|
1 | 7 | |
2 | 14 | |
3 | 15 | and is finally empty for every query |
4 | 13 | |
5 | 14 | is correct and finally empty for every query |
6 | 11 | is correct for every query |
7 | 10 | |
8 | 11 | |
9 | 5 |
Example 1
stdin
1
6 2
5 4 0 0 23 0
1 4
5 6
stdout
15
23
Explanation
For the first query, we will perform the operations from to in order. After the first operation, the stack looks like this: . . After the second operation, the stack looks like this: . . After the third operation, the stack looks like this: . . After the last operation, the stack is empty. . .
For the second query, we must perform operations and . After operation , the stack looks like this: . . After the last operation, the stack is empty. . .
Example 2
stdin
1
10 4
22 0 26 0 72 447 0 497 0 0
1 10
3 10
8 9
1 4
stdout
1208
1186
497
48
Explanation
For the first query,
For the second query,
For the third query,
For the last query,
Example 3
stdin
2
10 5
22 0 26 0 72 447 0 497 0 0
1 10
3 10
8 9
1 4
1 9
stdout
5538
4260
497
96
1984
Explanation
The values of maxstack for all subsequences are written in the table below. We leave the cells where (for which the operation is not defined) empty.