XOR love
Difficulty: Hard

Devendra loves the XOR operation very much which is denoted by $\wedge$ sign in most of the programming languages. He has a list $A$ of $N$ numbers and he wants to know the answers of $M$ queries. Each query will be denoted by three numbers i.e. $K,P,R$ .

For query $K,P \text{ and } R$, he has to print the value of the $KPRsum$ which can be described as given below. As the value of the $KPRsum$ can be large. So, print it modulus $(10^9 + 7)$.

$$ KPRsum = \sum_{i=P}^{R-1} \sum_{j=i+1}^{R} (K \oplus (A[i] \oplus A[j]) )$$

**Input Format**  
The first line contains an integer $N$, i.e., the number of the elements in the list. List is numbered from $1$ to $N$.  
Next line will contain $N$ space seperated integers.  
Third line will contain a number $M$ i.e. number of queries followed by $M$ lines each containing  integers $K,P \& R$.

**Output Format**  
Print $M$ lines, $i^{th}$ line will be answer of $i^{th}$ query. **Answer will be 0 in case of P=R.**

**Constraints**  
$1 \le N \le 10^5$  
$1 \le A[i] \le 10^6$  
$1 \le M \le 10^5$  
$0 \le K \le 10^6$  
$1 \le P \le R \le N$

**Sample Input**  

	3
    1 2 3
    2
    1 1 3
    2 1 3

    
**Sample Output**  

	5
    4

**Explanation**

For first query, it will will be $$(1 \oplus( 1 \oplus 2) ) + (1 \oplus( 1 \oplus 3) ) + (1 \oplus( 2 \oplus 3) )  =  5 $$

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
###C++  
```cpp  
#ifdef ssu1
#define _GLIBCXX_DEBUG
#endif
#undef NDEBUG

#include <algorithm>
#include <functional>
#include <numeric>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>

using namespace std;

#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template<typename T> T abs(T a) { return a < 0 ? -a : a; }
template<typename T> T sqr(T a) { return a*a; }

const int INF = (int)1e9;
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932384626433832795;

/*

    This is just to check correctness of input

*/
int readInt(int l, int r){
    int x;
    if(scanf("%d", &x) != 1){
        fprintf(stderr, "Expected int in range [%d, %d], but haven't found!", l, r);
        throw;
    }
    if(!(l <= x && x <= r)){
        fprintf(stderr, "Expected int in range [%d, %d], but found %d!", l, r, x);
        throw;
    }
    return x;
}
int readInt(int l, int r, string name){
    int x;
    if(scanf("%d", &x) != 1){
        fprintf(stderr, "Expected int %s in range [%d, %d], but haven't found!", name.c_str(), l, r);
        throw;
    }
    if(!(l <= x && x <= r)){
        fprintf(stderr, "Expected int %s in range [%d, %d], but found %d!", name.c_str(), l, r, x);
        throw;
    }
    return x;
}
li readLong(li l, li r){
    li x;
    if(scanf("%lld", &x) != 1){
        fprintf(stderr, "Expected long long in range [%lld, %lld], but haven't found!", l, r);
        throw;
    }
    if(!(l <= x && x <= r)){
        fprintf(stderr, "Expected long long in range [%lld, %lld], but found %lld!", l, r, x);
        throw;
    }
    return x;
}
li readLong(li l, li r, string name){
    li x;
    if(scanf("%lld", &x) != 1){
        fprintf(stderr, "Expected long long %s in range [%lld, %lld], but haven't found!", name.c_str(), l, r);
        throw;
    }
    if(!(l <= x && x <= r)){
        fprintf(stderr, "Expected long long %s in range [%lld, %lld], but found %lld!", name.c_str(), l, r, x);
        throw;
    }
    return x;
}
const ld __EPS__ = 1e-15;
ld readDouble(double l, double r){
    double x;
    if(scanf("%lf", &x) != 1){
        fprintf(stderr, "Expected double in range [%lf, %lf], but haven't found!", l, r);
        throw;
    }
    if(!(l <= x + __EPS__ && x <= r + __EPS__)){
        fprintf(stderr, "Expected double in range [%lf, %lf], but found %lf!", l, r, x);
        throw;
    }
    return x;
}
ld readDouble(double l, double r, string name){
    double x;
    if(scanf("%lf", &x) != 1){
        fprintf(stderr, "Expected double %s in range [%lf, %lf], but haven't found!", name.c_str(), l, r);
        throw;
    }
    if(!(l <= x + __EPS__ && x <= r + __EPS__)){
        fprintf(stderr, "Expected double %s in range [%lf, %lf], but found %lf!", name.c_str(), l, r, x);
        throw;
    }
    return x;
}

const int LOG = 22;
const int NMAX = 100000;
const int VMAX = 1000000;

int bit(int v, int i){
    return (v >> i) & 1;
}

int z[LOG][NMAX], o[LOG][NMAX], n;

void upd(int t[NMAX], int i, int di){
    for(; i < n; i = (i | (i + 1)))
        t[i] += di;
}

int sum(int t[NMAX], int r){
    int ans = 0;
    for(; r >= 0; r = (r & (r + 1)) - 1)
        ans += t[r];
    return ans;
}

int sum(int t[NMAX], int l, int r){
    return sum(t, r) - sum(t, l - 1);
}

int main(){
#ifdef ssu1
    assert(freopen("input.txt", "rt", stdin));
    //assert(freopen("output.txt", "wt", stdout));
#endif
    n = readInt(1, NMAX, "n");
    forn(i, n){
        int cur = readInt(1, VMAX, "a[i]");
        forn(j, LOG){
            if(bit(cur, j))
                upd(o[j], i, 1);
            else
                upd(z[j], i, 1);
        }
    }
    int m = readInt(1, 100000, "m");
    forn(mi, m){
        int K, l, r;
        K = readInt(0, VMAX);
        l = readInt(1, n) - 1;
        r = readInt(1, n) - 1;
        assert(l <= r);

        li ans = 0;
        forn(i, LOG){
            int cnt[2];
            cnt[0] = sum(o[i], l, r);
            cnt[1] = sum(z[i], l, r);
//            if(i == 0)
//                cerr << cnt[0] << " " << cnt[1] << endl;

            forn(b1, 2){
                forn(b2, b1 + 1){
                    int result = b1 ^ b2 ^ bit(K, i);
                    if(result == 0)
                        continue;
                    li ways = 0;
                    if(b1 == b2)
                        ways = (cnt[b1] * 1LL * (cnt[b1] - 1)) / 2;
                    else
                        ways = cnt[b1] * 1LL * cnt[b2];
                    ans += ways * (1LL << i);
                }
            }
        }

        printf("%lld\n", ans % (int(1e9) + 7));
    }

    return 0;
}
```