Task
Little Green Heart has gifted Little Purple Heart for Valentine's Day two arrays A and B of size which together contain the numbers from to exactly once.
Little Purple Heart performs the following operation:
- She chooses indices l and r with .
- She creates an array C of size , such that for every i with she chooses either or .
- She calculates the value , which is equal to the number of indices i with such that there is no index j with and .
Little Green Heart, being the computer scientist that he is, wants to impress Little Purple Heart by computing the sum of for all the arrays that she could have constructed. Since this number can be quite large, he wants to compute it modulo . Moreover, he wants to do so for Q different operations.
Formally, given Q queries of the form , , he defines S as the set of all the arrays C that Little Purple Heart could have built. Then, for each query, he wants to calculate
Your task is to help Little Green Heart impress Little Purple Heart by computing the answer to each query.
Input data
The first line of the input contains the number . The second line contains numbers representing the array A. The third line contains numbers representing the array B. Together, these 2 arrays contain the numbers from to exactly once.
The fourth line contains the number of queries . Each of the following lines contains 2 numbers describing a query of the form l r.
Output data
The output should contain lines representing the answers to all queries , each on a new line, computed modulo .
Constraints and clarifications
- .
- .
| # | Points | Constraints |
|---|---|---|
| 1 | 5 | , |
| 2 | 16 | |
| 3 | 9 | for all with |
| 4 | 17 | |
| 5 | 11 | |
| 6 | 14 | , |
| 7 | 10 | |
| 8 | 18 | No further restrictions |
Example 1
stdin
6
2 10 3 4 5 6
11 1 9 8 7 12
2
3 6
1 2
stdout
37
5
Explanation
There are 2 queries.
For the first query, we will consider the subsequences and when constructing C. Next we consider all possible options for C and sum . For example, for , the indexes have the property that there is no such that . Therefore, .
Summing across all arrays C that can be constructed, we get .
For the second query, we can construct 4 arrays C:
Example 2
stdin
12
11 10 7 23 1 18 22 16 8 14 20 6
3 4 9 13 19 5 24 12 15 21 17 2
1
7 11
stdout
32
Explanation
There is one query. Computing .