electicode
HomeCoursesResourcesProblemsNational OlympiadContestsLeaderboard
...

Arrays and Queries

Time Limit: 1500msMemory Limit: 256MB
View EditorialView Submissions

Problem Description

Given an integer nnn and an array aaa of nnn integers. The process of constructing the array bbb is defined as follows.

Initially, the array bbb is empty. The process is carried out in nnn consecutive rounds. In the iii-th round, the new array bbb is defined as the concatenation of the previous version of the array bbb, the element aia_iai​, and again the previous version of the array .

Formally, in each iii-th round, the array bbb is updated according to the rule:

b:=b+[ai]+bb := b + [a_i] + bb:=b+[ai​]+b,

where the symbol «+++» denotes the concatenation of arrays, and [ai][a_i][ai​] is an array consisting of a single element aia_iai​.

After the construction of the array bbb is completed, it is necessary to process qqq queries. Each query is defined by a pair of integers (l,r)(l, r)(l,r). For each query, it is required to find the sum of the elements of the array bbb from position lll to position rrr inclusive modulo .

Input format

In the first line, an integer nnn (1≤n≤60)(1 \le n \le 60)(1≤n≤60) is given --- the number of elements in the array aaa.

In the second line, nnn integers are given a1,a2,…,ana_1, a_2, \ldots, a_na1​,a2​,…,an​ --- the elements of the array .

In the third line, an integer qqq (1≤q≤2⋅105)(1 \le q \le 2 \cdot 10^5)(1≤q≤2⋅105) is given --- the number of queries.

In the following qqq lines, pairs of integers lll and rrr are given (1≤l≤r≤2n−1)(1 \le l \le r \le 2^n - 1)(1≤l≤r≤2n.

Output format

For each query, output a single number --- the sum of the elements of the array bbb on the segment [l,r][l, r][l,r] modulo 109+710^9 + 7109+7. Output each answer on a separate line.

Scoring

SubtaskAdditional constraintsPointsRequired Subtasks
000Sample000—
111All elements of the array aia_ia are equal to each other

Examples

Example 1
Input
3
2 0 5
6
1 4
1 7
3 5
6 6
3 7
4 7
Output
9
13
9
0
11
9

© 2026 Electicode. All rights reserved.

bb
b
109+710^9 + 7
109+7
(0≤ai≤109)(0 \le a_i \le 10^9)
(0≤ai​≤109)
aaa
−
1)
i
​
888
—
222l=1l = 1l=1, r=2k−1r = 2^k - 1r=2k−1, where 1≤k≤n1 \le k \le n1≤k≤n171717—
333n≤20n \le 20n≤20232323—
444For all queries, l=rl = rl=r212121—
555—3131310,1,2,3,40,1,2,3,40,1,2,3,4