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 JavaScript (node.js v5.12) (judge verdict: Wrong Answer)

"use strict";
var GET=(function(){function f(s){return new g(s);}function g(s){this._s=s.trim().split("\n");this._y=0;}g.prototype.a=function(f){var s=this._s, y=this._y, r;if(typeof s[y]==="string")s[y]=s[y].split(" ").reverse();r=s[y].pop();if(!s[y].length)this._y++;return f?r:+r;};g.prototype.l=function(f){var s=this._s[this._y++].split(" ");return f?s:s.map(a=>+a);};g.prototype.m=function(n,f){var r=this._s.slice(this._y,this._y+=n).map(a=>a.split(" "));return f?r:r.map(a=>a.map(a=>+a));};g.prototype.r=function(n,f){var r=this._s.slice(this._y,this._y+=n);return f?r:r.map(a=>+a);};return f;})();
var o=GET(require("fs").readFileSync("/dev/stdin","utf8"));
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill(0).map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill(0).map(x=>"+r+")";return eval(r);}
var mod = 1e9+7;
function mul(){for(var a=arguments,r=a[0],i=a.length;--i;)r=((r>>16)*a[i]%mod*65536+(r&65535)*a[i])%mod;return r;}

var bit = xArray(0,60);
var y = 30;
var z = 1 << y;

console.log(main());
function main(){
  var n = o.a();
  var a = o.l(1);
  for(var i = 0; i < n; i++){
    if(a[i] > 9e15){
      big(a[i]);
    }else{
      small(+a[i],0);
    }
  }
  var ans = 0, x = 1;
  for(var i = 0; i < 60; i++){
    ans += mul(x,bit[i],n-bit[i]);
    x = x*2%mod;
  }
  return ans%mod;
}

function small(x,j){
  while(x > 4e9){
    if(x&1)bit[j]++;
    x = Math.floor(x * 0.5);
    j++;
  }
  while(x){
    if(x&1)bit[j]++;
    x >>>= 1;
    j++;
  }
}

function big(x){
  var h = +x.slice(0,-5);
  var l = +x.slice(-5);
  var u = h / z | 0;
  var v = h - u * z;
  u *= 1e5;
  l += v * 1e5;
  v = l / z | 0;
  u += v;
  v = l - v * z;
  for(var i = 0; u; i++, u >>>= 1){
    if(u&1)bit[i]++;
  }
  small(v,y);
}