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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;

using SyrupBeans.Competitive.Integer;

using E = System.Linq.Enumerable;

public class Program
{
    public static void Main()
    {
        var n = int.Parse(Console.ReadLine());
        var seqA = Console.ReadLine().Split().Select(x => long.Parse(x)).ToArray();

        var d = 0;
        for (var max = seqA.Max(); max > 0; max >>= 1)
        {
            ++d;
        }

        var sum = new long[d];
        for (int i = 0; i < seqA.Length; ++i)
        {
            for (int j = 0; j < sum.Length; ++j)
            {
                sum[j] += seqA[i] >> d - (j + 1) & 1;
            }
        }

        var k = new Modular(0, (long)1e9 + 7);
        for (int i = 0; i < sum.Length; ++i)
        {
            k *= 2;
            k += sum[i] * (seqA.Length - sum[i]);
        }

        Console.WriteLine(k.Remainder);
    }
}

namespace SyrupBeans.Competitive.Integer
{
    using Integer = System.Int64;

    using System;

    public static class Divisibility
    {
        /// <summary>O(log min(<paramref name="x" />, <paramref name="y" />))</summary>
        public static Integer FindGcd(Integer x, Integer y) => x < 1 ? y : FindGcd(y % x, x);
    }

    public struct Modular
    {
        public Integer Modulus { get; private set; }
        public Integer Remainder { get; private set; }

        public Modular(Integer natural, Integer modulus)
        {
            Remainder = natural < modulus ? natural : natural % modulus;
            Modulus = modulus;
        }
        public Modular(Integer natural)
        {
            Remainder = 0;
            Modulus = natural;
        }

        /// <summary>O(log min(<paramref name="a" />, <paramref name="b" />))</summary>
        /// <returns>Satisfying values with the equation: <paramref name="a" />X + <paramref name="b" />Y = Gcd .</returns>
        public static Tuple<Integer, Integer, Integer> FindBézoutIdentity(Integer a, Integer b)
        {
            Integer x = 1, y = 0;
            Integer u = 0, v = 1;
            while (b > 0)
            {
                var k = a / b;
                a = b;
                b = a - k * b;
                x = u; u = 1 * x - k * u;
                y = v; v = 1 * y - k * v;
            }
            return Tuple.Create(x, y, a);
        }

        public Modular AdditiveInversion => new Modular(Modulus - Remainder, Modulus);
        public Modular? MultiplicativeInverse {
            get
            {
                var t = FindBézoutIdentity(this.Remainder, this.Modulus);
                return t.Item3 > 1 ? (Modular?)null : new Modular(t.Item1, this.Modulus);
            }
        }

        public Modular AddWith(Integer x) => new Modular(Remainder + x, Modulus);
        public Modular SubtractWith(Integer x) => new Modular(Remainder - x + Modulus, Modulus);
        public Modular MultiplyWith(Integer x) => new Modular(Remainder * x, Modulus);
        public Modular DivideWith(Integer x) => new Modular(Remainder / x, Modulus / Divisibility.FindGcd(Modulus, x));
        public Modular PowerWith(Integer x) => new Modular((Integer)System.Numerics.BigInteger.ModPow(Remainder, x, Modulus), Modulus);

        public static Modular operator +(Modular x, Integer y) => x.AddWith(y);
        public static Modular operator -(Modular x, Integer y) => x.SubtractWith(y);
        public static Modular operator *(Modular x, Integer y) => x.MultiplyWith(y);
        public static Modular operator /(Modular x, Integer y) => x.DivideWith(y);
        public static Modular Pow(Modular x, Integer y) => x.PowerWith(y);
    }
}