Problem:
Score : 400 points

Problem Statement

We have N integers. The i-th integer is A_i.

Find \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

2 \leq N \leq 3 \times 10^5

0 \leq A_i < 2^{60}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the value \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

Sample Input 1

3
1 2 3

Sample Output 1

6

We have (1\mbox{ XOR } 2)+(1\mbox{ XOR } 3)+(2\mbox{ XOR } 3)=3+2+1=6.

Sample Input 2

10
3 1 4 1 5 9 2 6 5 3

Sample Output 2

237

Sample Input 3

10
3 14 159 2653 58979 323846 2643383 27950288 419716939 9375105820

Sample Output 3

103715602

Print the sum modulo (10^9+7).

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

// iterator
#define REP(i,from, to) for(lli i=from;i<to;i++)
#define REPE(i,from, to) for(lli i=from;i<=to;i++)
#define REP_R(i,from, to) for(lli i=from;i>to;i--)
#define REPE_R(i,from, to) for(lli i=from;i>=to;i--)
#define REPIT(it,container) for(auto it = container.begin(); it != container.end(); it++)
#define REPIT_R(it,container) for(auto it = container.rbegin(); it != container.rend(); it++)

// input
#define cin1(x)             cin >> x
#define cin2(x, y)          cin >> x >> y
#define cin3(x, y, z)       cin >> x >> y >> z
#define ncin1(n, x)         REP(i, 0, n) { cin1(x[i]); }
#define ncin2(n, x, y)      REP(i, 0, n) { cin2(x[i], y[i]); }
#define ncin3(n, x, y, z)   REP(i, 0, n) { cin3(x[i], y[i], z[i]); }
#define ncinp(n, p)         REP(i, 0, n) { lli p1,p2; cin >> p1 >> p2; p[i] = P(p1, p2); }
#define ncin1_1(n, x)       REPE(i, 1, n) { cin1(x[i]); }
#define ncin2_1(n, x, y)    REPE(i, 1, n) { cin2(x[i], y[i]); }
#define ncin3_1(n, x, y, z) REPE(i, 1, n) { cin3(x[i], y[i], z[i]); }
#define ncinp_1(n, p)       REPE(i, 1, n) { lli p1,p2; cin >> p1 >> p2; p[i] = P(p1, p2); }

// output
#define cout1(x)         cout << #x << ": " << x << endl;
#define cout2(x, y)      cout << #x << ": " << x << ", " << #y << ": " << y << endl;
#define cout3(x, y, z)   cout << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << endl;;
#define ncout1(n, x)     REP(i, 0, n) {cout << #x << "[" << i << "]: "<< x[i] << endl;}

#define coutp(p)         cout << #p << ":" <<  " (" << p.first << ", " << p.second << ")" << endl;

// sort
#define sort_r(x, y)        sort(x, y, greater<lli>()); // 降順(5,4,3,,,)

#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))

typedef unsigned long long int lli;
typedef pair<lli, lli> P;
typedef tuple<lli, lli, lli> tup;
typedef vector<lli> vlli;
// 繰り返し二乗法
// N^p mod m
lli mod_pow(lli n, lli p, lli mod) {
    lli res = 1;
    while (p > 0) {
        if (p & 1) {
            res = res * n % mod;
        }
        n = n * n % mod;
        p >>= 1;
    }
    return res;
}

int main() {
    lli n;
    cin1(n);
    lli a[n];
    ncin1(n,a);

    lli zero_count[61] = {0};
    lli one_count[61] = {0};
    REP(i,0,n) {
        lli x = a[i];
        REP(j,0,61) {
            //// j けため
            //if (a[i] & (1<<j)) {
            //    one_count[j]++;
            //    cout2(j, one_count[j]);
            //}
            //else {
            //    zero_count[j]++;
            //    cout2(j, zero_count[j]);
            //}
            // j けため
            if (x & 1) {
                one_count[j]++;
                //cout2(j, one_count[j]);
            }
            else {
                zero_count[j]++;
                //cout2(j, zero_count[j]);
            }
            x = x>>1;
        }
    }
    //REP(i,0,5) {
    //    cout3(i, zero_count[i], one_count[i]);
    //}

    lli mod = 1000000007;
    lli ans = 0;
    REP(i,0,n) {
        // Ai〜
        lli x = a[i];
        REP(j,0,61) {
            if (one_count[j] == 0 || zero_count[j] == 0)
                continue;
            if (x & 1) {
                // その値が1
                one_count[j]--;
                //cout3(i,j,((1<<j)*zero_count[j]));
                //cout2(zero_count[j], one_count[j]);
                //ans += (1<<j)*zero_count[j];
                //ans += (((1<<j) % mod)*zero_count[j]) % mod;
                ans += mod_pow(2, j, mod) * zero_count[j] % mod;
            }
            else {
                zero_count[j]--;
                //cout3(i,j,((1<<j)*one_count[j]));
                //cout2(zero_count[j], one_count[j]);
                //ans += (1<<j)*one_count[j];
                //ans += (((1<<j) % mod)*one_count[j]) % mod;
                ans += mod_pow(2, j, mod) * one_count[j] % mod;
            }
            x = (x>>1);
        }
    }
    cout << ans << endl;
}