Piggy Calculator
题号:NC221766
时间限制:C/C++/Rust/Pascal 3秒,其他语言6秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

Piggy is developing a fantastic calculator. This calculator innovatively invents a binary operator symbol that perfectly mixes addition and multiplication, which we call   (k is a positive integer).

The value of  is equal to . The precedence between operators is that the higher the k, the higher the priority. The higher priority goes first. For example,has a higher priority than , so you must calculate  before . The operators are left-associative. Between the operators with the same priority, you should calculate them from left to right. For example, in the expression , you must calculate firstly.

Now, Piggy wants to ask you to help him implement a function. There is an expression with n numbers and n-1 operators. At the same time, there are q queries, each of them shaped as (L,R). You need to calculate the result of the sub-expression formed by the L-th number to the R-th number.

输入描述:

The first line contains one positive integer n  -- the length of the expression.

The second line contains n positive integers   -- the n numbers from left to right in the expression.

The third line contains n-1 positive integers  , where the i-th operator from left to right is  and is  between the i-th number and the -th number.

The forth line contains one positive integer q  -- the number of queris.

The next q lines describe the queries, where the i-th line contains two integers L_i, R_i .

输出描述:

For each query, print an integer in one line -- the result of the calculation. Since the answer may be very large, you only need to print the answer modulo .

示例1

输入

复制
5
1 2 3 4 5
5 2 3 4
2
1 5
2 4

输出

复制
264
46

说明

In the first query, the equation is 1 \odot^5 2 \odot^2 3 \odot^3 4 \odot^4 5 = 15 \odot^2 3 \odot^3 4 \odot^4 5 = 15 \odot^2 3 \odot^3 36 = 15 \odot^2 117 = 264.

In the second query, the equation is 2 \odot^2 3 \odot^3 4 = 2 \odot^2 21 = 46.