This page is still under construction.

Parts of this page are still being built. What you see may change.

AND vs OR

Time limit2sMemory limit1024 MB

Summary
For each query range, sum the positive values of (first AND last) minus the OR of the inner elements over all subarrays, modulo 1000000007.
Level

Hard8 of 10

Topics
Bit manipulation, Divide and conquer, Prefix sum
Solved
No attempts yet

Problem

The value of a sequence al,al+1,⋯ ,ara_l,a_{l+1},\cdots,a_r is defined as follows.

  • If the length is 2 or less, the value of the sequence is 00.
  • If the length is 3 or more, the value of the sequence is (al & ar)−(al+1 ∣ al+2 ∣⋯∣ ar−1)(a_l \, \And \, a_r) - (a_{l+1} \, | \, a_{l+2} \, | \cdots | \, a_{r-1}).

&\And is the bitwise and operator, and ∣| is the bitwise or operator.

You are given a sequence of integers a1,a2,⋯ ,aNa_1,a_2,\cdots,a_N.

Write a program that processes the following query QQ times.

  • ii jj : Find the sum of all positive values among the values of every contiguous subsequence of ai,ai+1,⋯ ,aja_i,a_{i+1},\cdots,a_j, and print it. (1≤i≤j≤N)(1\leq i \leq j \leq N)

Input

The first line contains an integer NN. (1≤N≤1 000 000)(1\leq N \leq 1\,000\,000)

The second line contains the sequence a1,a2,⋯ ,aNa_1,a_2,\cdots,a_N as integers separated by spaces. (0≤ai≤1018)(0\leq a_i \leq 10^{18})

The third line contains an integer QQ, the number of queries. (1≤Q≤1 000 000)(1\leq Q \leq 1\,000\,000)

From the fourth line, the next QQ lines each contain two integers ii and jj separated by a space. (1≤i≤j≤N)(1\leq i \leq j \leq N)

Output

For each query, print the answer on its own line.

Since the answer can be very large, print it modulo 109+710^9+7.

Examples1

  1. Example 1

    Input
    4
    11 2 8 15
    3
    1 3
    1 2
    1 4
    
    Expected output
    6
    0
    7