Sum of Pairwise XORs

easy bitmask bit-manipulation math combinatorics 1000 ms · 256 MB

Sum of Pairwise XORs

You are given an array of n non-negative integers a_1, a_2, ..., a_n.

Compute the sum of a_i XOR a_j over all pairs of indices 1 ≤ i < j ≤ n, where XOR is the bitwise exclusive or. Each unordered pair of positions is counted exactly once, even if the two values are equal.

The sum can be very large, so print it modulo 10^9 + 7.

Input

The first line contains a single integer n (1 ≤ n ≤ 3·10^5).

The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i < 2^30).

Output

Print a single integer: the sum of a_i XOR a_j over all pairs i < j, taken modulo 10^9 + 7.

Notes

In the first example the pairs give 1 XOR 2 = 3, 1 XOR 3 = 2 and 2 XOR 3 = 1, for a total of 6.

In the second example the six pairs give 7 + 4 + 9 + 3 + 14 + 13 = 50.

If n = 1 there are no pairs and the answer is 0.

Example 1

Input

3
1 2 3

Output

6

Example 2

Input

4
0 7 4 9

Output

50

Hints · reveal one at a time if you're stuck

Hint 1

Checking all pairs is about 4.5·10^10 operations, which is too slow. XOR works on each bit independently, so how does a single bit contribute to the total over all pairs?

Hint 2

For each bit, count how many numbers have that bit set. A pair contributes to that bit exactly when its two numbers differ there. Be careful with overflow: the exact total is larger than a 64-bit integer, so reduce modulo 10^9 + 7 as you add each term.

report an issue Prefer your own editor? sooks prep 11 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.