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)

use std::io::Write;
use std::ops::{Add, Mul, Sub};

#[allow(unused)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

fn main() {
    let n: usize = {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        let s = s.trim_right().to_owned();
        s.parse().unwrap()
    };


    let arr: Vec<usize> = {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        let s = s.trim_right().to_owned();
        s.split_whitespace().map(|x| x.parse().unwrap()).collect()
    };

    let mut ans = Modulo(0);
    for i in 0..60 {
        let bit = 1 << i;
        let mut count = 0;
        for &a in &arr {
            if a & bit > 0 {
                count += 1;
            }
        }

        let tmp = Modulo(count) * Modulo(n - count) * Modulo(2).pow(i);
        ans = ans + tmp;
    }

    println!("{}", ans.0);
}

fn positive_rem(a: isize, b: usize) -> usize {
    let b = b as isize;
    let mut value = a % b;
    if value < 0 {
        value += b;
    }
    // TODO: TryFrom
    value as usize
}

/// Return (x, y) s.t. ax + by = d where d = gcd(a, b)
fn ext_gcd(a: usize, b: usize) -> (isize, isize) {
    if b == 0 {
        return (1, 0);
    }

    let q = (a / b) as isize;
    let r = a % b;
    let (x1, y1) = ext_gcd(b, r);
    (y1, x1 - q * y1)
}

#[derive(Debug, Copy, Clone)]
struct Modulo(usize);

const MODULO: usize = 1_000_000_000 + 7;

impl Modulo {
    fn from_isize(n: isize) -> Modulo {
        // TODO: use TryFrom
        Modulo(positive_rem(n, MODULO as usize))
    }

    fn pow(self, p: usize) -> Modulo {
        if self == Modulo(0) {
            return Modulo(0);
        }
        if p == 0 {
            return Modulo(1);
        }

        if p % 2 == 0 {
            let half = self.pow(p / 2);
            half * half
        } else {
            self.pow(p - 1) * self
        }
    }

    // when MODULO is prime
    fn inv(self) -> Modulo {
        let (x, _) = ext_gcd(self.0 as usize, MODULO as usize);
        Modulo::from_isize(x)
    }

    // when MODULO is prime
    fn binom_coef(n: isize, k: isize) -> Modulo {
        let mut ret = Modulo::from_isize(1);
        for i in 1..(k + 1) {
            ret = ret * Modulo::from_isize(n - i + 1) * Modulo::from_isize(i).inv();
        }
        ret
    }
}

impl Add for Modulo {
    type Output = Modulo;

    fn add(self, other: Modulo) -> Modulo {
        Modulo((self.0 + other.0) % MODULO)
    }
}

impl Sub for Modulo {
    type Output = Modulo;

    fn sub(self, other: Modulo) -> Modulo {
        Modulo(positive_rem(
            self.0 as isize - other.0 as isize,
            MODULO as usize,
        ))
    }
}

impl Mul for Modulo {
    type Output = Modulo;

    fn mul(self, other: Modulo) -> Modulo {
        Modulo((self.0 * other.0) % MODULO)
    }
}

impl PartialEq for Modulo {
    fn eq(&self, &other: &Self) -> bool {
        self.0 == other.0
    }
}

impl Eq for Modulo {}