Task
Stefan has big plans for the year that just begun and among others, one of his plans is to improve his contest ratings.
While every competitive programmer dreams of this, he is perfectly aware that not everyone can achieve it.
Now he stumbled across an array that he received as a gift many years ago, together with a very old scroll containing the problem statement below.
You are given an array of positive integers, numbered from to , inclusive.
You have to answer queries: given , and , find the product of the largest values of .
Stefan realized that this is not a cakewalk question so now it's your turn to help him overcome this challenge and write a program that answers the queries for him.
Since the answers can be very big, you have to print them module (i.e. the reminder of the division by ).
Input data
The input file consists of:
- a line containing integers , .
- a line containing the integers .
- lines, the -th of which consisting of integers , , .
Output data
The output file must contain a single line consisting of the integers .
Constraints and clarifications
- .
- for each .
- for each query.
- for each query.
# | Points | Constraints |
---|---|---|
1 | 0 | Examples. |
2 | 11 | |
3 | 13 | for all queries |
4 | 19 | for all queries |
5 | 25 | |
6 | 32 | No additional limitations. |
Example
stdin
9 15
8 1 9 4 7 9 4 9 3
7 7 1
1 4 2
5 6 1
1 7 4
2 8 6
2 3 1
3 6 2
5 7 3
0 7 3
0 5 3
6 8 3
2 5 1
0 7 6
2 3 2
4 7 4
stdout
9
63
9
5103
81648
9
63
324
729
648
108
9
163296
36
2268
Explanation
For the second query of the sample case, the values between and are , the largest values are and and their product is .
For the fourth query of the sample case, the values between and are , the largest values are , , and and their product is .