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.42.0) (judge verdict: Accepted)

#![allow(clippy::needless_range_loop)]
#![allow(unused_macros)]
#![allow(dead_code)]

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        #[allow(unused_mut)]
        let mut 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")
    };
}

const MOD: usize = 1000000007;
#[derive(Copy, Clone, Eq, PartialEq, std :: fmt :: Debug)]
struct ModInt(usize);
impl std::ops::Add for ModInt {
    type Output = Self;
    fn add(self, rhs: Self) -> Self {
        ModInt((self.0 + rhs.0) % MOD)
    }
}
#[allow(clippy::suspicious_op_assign_impl)]
impl std::ops::AddAssign for ModInt {
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
        self.0 %= MOD;
    }
}
impl std::ops::Sub for ModInt {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        ModInt((self.0 + MOD - rhs.0) % MOD)
    }
}
#[allow(clippy::suspicious_op_assign_impl)]
impl std::ops::SubAssign for ModInt {
    fn sub_assign(&mut self, rhs: Self) {
        self.0 += MOD - rhs.0;
        self.0 %= MOD;
    }
}
impl std::ops::Mul for ModInt {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self {
        ModInt((self.0 * rhs.0) % MOD)
    }
}
#[allow(clippy::suspicious_op_assign_impl)]
impl std::ops::MulAssign for ModInt {
    fn mul_assign(&mut self, rhs: Self) {
        self.0 *= rhs.0;
        self.0 %= MOD;
    }
}
impl std::ops::Div for ModInt {
    type Output = Self;
    fn div(self, rhs: Self) -> Self {
        ModInt((self.0 * rhs.pow(MOD - 2).0) % MOD)
    }
}
#[allow(clippy::suspicious_op_assign_impl)]
impl std::ops::DivAssign for ModInt {
    fn div_assign(&mut self, rhs: Self) {
        self.0 *= rhs.pow(MOD - 2).0;
        self.0 %= MOD;
    }
}
impl std::fmt::Display for ModInt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0.to_string())
    }
}
impl ModInt {
    fn pow(&self, power: usize) -> ModInt {
        let mut acc_base = *self;
        let mut acc_pow = power;
        let mut res = ModInt(1);
        while acc_pow > 0 {
            if acc_pow & 1 == 1 {
                res *= acc_base;
            }
            acc_base *= acc_base;
            acc_pow >>= 1;
        }
        res
    }
}
struct ModIntFact {
    memo: Vec<ModInt>,
    memo_inv: Vec<ModInt>,
}
impl ModIntFact {
    fn new(size: usize) -> Self {
        let mut memo = vec![ModInt(0); size + 1];
        memo[0] = ModInt(1);
        for n in 1..=size {
            memo[n] = memo[n - 1] * ModInt(n);
        }
        let memo_inv = memo.iter().map(|x| x.pow(MOD - 2)).collect();
        Self { memo, memo_inv }
    }
    fn fact(&self, n: usize) -> ModInt {
        self.memo[n]
    }
    fn fact_inv(&self, n: usize) -> ModInt {
        self.memo_inv[n]
    }
    fn ncr(&self, n: usize, r: usize) -> ModInt {
        self.memo[n] * self.memo_inv[r] * self.memo_inv[n - r]
    }
}
impl std::iter::Sum for ModInt {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(ModInt(0), |x, y| x + y)
    }
}

fn main() {
    input! {
        n: usize,
        a: [usize; n],
    }
    let ans: ModInt = (0usize..=59).map(|e| {
        let bit = 1 << e;
        let set = a.iter().filter(|x| **x & bit > 0).count();
        ModInt(set * (n - set) % MOD) * ModInt(bit % MOD)
    }).sum();
    println!("{}", ans);
}