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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_imports)]

use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    }
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

macro_rules! chmax {
    ($x:expr, $e:expr) => {
        if $x < $e {
            $x = $e;
        }
    };
}
macro_rules! chmin {
    ($x:expr, $e:expr) => {
        if $x > $e {
            $x = $e;
        }
    };
}

use std::ops::*;

const MOD: usize = 1000000007;
#[derive(Copy, Clone)]
struct ModInt {
    n: usize,
}

impl ModInt {
    fn from_usize(n: usize) -> ModInt {
        ModInt { n: n % MOD }
    }
    fn pow(self, power: usize) -> ModInt {
        if power == 0 {
            return ModInt::from_usize(1);
        }
        if power % 2 == 0 {
            let t = self.pow(power/2);
            return t * t;
        }
        self * self.pow(power-1)
    }
    fn inv(self) -> ModInt {
        self.pow(MOD - 2)
    }
}

fn mod_int(n: usize) -> ModInt {
    ModInt::from_usize(n)
}

impl Add for ModInt {
    type Output = ModInt;

    fn add(self, rhs: ModInt) -> ModInt {
        let mut tmp = self;
        tmp.n += rhs.n;
        if tmp.n >= MOD {
            tmp.n -= MOD;
        }
        tmp
    }
}

impl AddAssign for ModInt {
    fn add_assign(&mut self, rhs: ModInt) {
        *self = *self + rhs;
    }
}

impl Sub for ModInt {
    type Output = ModInt;

    fn sub(self, rhs: ModInt) -> ModInt {
        let mut tmp = self;
        if tmp.n < rhs.n {
            tmp.n += MOD;
        }
        tmp.n -= rhs.n;
        tmp
    }
}

impl SubAssign for ModInt {
    fn sub_assign(&mut self, rhs: ModInt) {
        *self = *self - rhs;
    }
}

impl Mul for ModInt {
    type Output = ModInt;

    fn mul(self, rhs: ModInt) -> ModInt {
        let mut tmp = self;
        tmp.n *= rhs.n;
        tmp.n %= MOD;
        tmp
    }
}

impl MulAssign for ModInt {
    fn mul_assign(&mut self, rhs: ModInt) {
        *self = *self * rhs;
    }
}

impl Div for ModInt {
    type Output = ModInt;

    fn div(self, rhs: ModInt) -> ModInt {
        let mut tmp = self;
        tmp.n *= rhs.inv().n;
        tmp.n %= MOD;
        tmp
    }
}

impl DivAssign for ModInt {
    fn div_assign(&mut self, rhs: ModInt) {
        *self = *self / rhs;
    }
}

const INF: i64 = 1 << 60;

fn main() {
    input!{
        N: usize,
        A: [usize; N],
    }
    let mut ans = mod_int(0);
    for d in 0..60 {
        let mut ones = mod_int(0);
        let mut zeros = mod_int(0);
        for i in 0..N {
            if A[i] & 1<<d != 0 {
                ones += mod_int(1);
            } else {
                zeros += mod_int(1);
            }
        }
        ans += ones * zeros * mod_int(1<<d);
    }
    println!("{}", ans.n);
}