Hearts

Time limit: 1.5s Memory limit: 256MB Input: Output:

Task

Little Green Heart has gifted Little Purple Heart for Valentine's Day two arrays A and B of size NN which together contain the numbers from 11 to 2×N2 \times N exactly once.

Little Purple Heart performs the following operation:

  1. She chooses 22 indices l and r with 1lrN1 \leq l \leq r \leq N.
  2. She creates an array C of size k=rl+1k = r - l + 1, such that for every i with 0i<k0 \leq i < k she chooses either Ci=Al+iC_i = A_{l + i} or Ci=Bl+iC_i = B_{l + i}.
  3. She calculates the value val(C)\text{val}(C), which is equal to the number of indices i with 0i<k0 \leq i < k such that there is no index j with 0j<i0 \leq j < i and Cj>CiC_j > C_i.

Little Green Heart, being the computer scientist that he is, wants to impress Little Purple Heart by computing the sum of val(C)\text{val}(C) for all the arrays CC that she could have constructed. Since this number can be quite large, he wants to compute it modulo 109+710^9 +7. Moreover, he wants to do so for Q different operations.

Formally, given Q queries of the form (l,r)(l, r), 1lrN1 \leq l \leq r \leq N, 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

P(l,r)=CSval(C)mod109+7P(l, r)= \sum_{C \in S} \text{val}(C) \mod 10^9 + 7

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 NN. The second line contains NN numbers A1,A2,,AnA_1, A_2, \ldots, A_n representing the array A. The third line contains NN numbers B1,B2,,BnB_1, B_2, \ldots, B_n representing the array B. Together, these 2 arrays contain the numbers from 11 to 2×N2 \times N exactly once.

The fourth line contains the number of queries QQ. Each of the following QQ lines contains 2 numbers describing a query of the form l r.

Output data

The output should contain QQ lines representing the answers to all queries P(l,r)P(l, r), each on a new line, computed modulo 109+710^9 + 7.

Constraints and clarifications

  • 1N70 0001 \leq N \leq 70 \ 000.
  • 1Q70 0001 \leq Q \leq 70 \ 000.
# Points Constraints
1 5 1N171 \leq N \leq 17, 1Q1001 \leq Q \leq 100
2 16 1N,Q3001 \leq N, Q \leq 300
3 9 Bi=Ai+1B_i = A_i + 1 for all ii with 1iN1 \leq i \leq N
4 17 1N,Q10 0001 \leq N, Q \leq 10 \ 000
5 11 1Q2001 \leq Q \leq 200
6 14 1N50 0001 \leq N \leq 50 \ 000, 1Q10 0001 \leq Q \leq 10 \ 000
7 10 1N,Q50 0001 \leq N, Q \leq 50 \ 000
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 A3,A4,A5,A6A_3, A_4, A_5, A_6 and B3,B4,B5,B6B_3, B_4, B_5, B_6 when constructing C. Next we consider all possible options for C and sum val(C)\text{val}(C). For example, for C=[3,8,7,12]C=[3, 8, 7, 12], the indexes i=0,1,3i=0,1,3 have the property that there is no 0j<i0 \leq j < i such that Cj>CiC_j>C_i. Therefore, val(C)=3\text{val}(C)=3.

Summing val(C)\text{val}(C) across all arrays C that can be constructed, we get P(3,6)=37P(3,6)=37.

For the second query, we can construct 4 arrays C:

P(1,2)=val([2,10])+val([2,1])+val([11,10])+val([11,1])=2+1+1+1=5.P(1,2) = \text{val}([2,10])+\text{val}([2,1])+\text{val}([11,10])+\text{val}([11,1]) = 2 + 1 + 1 + 1 = 5.

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 P(7,11)=32P(7, 11) =32.

Log in or sign up to be able to send submissions!