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: Wrong Answer)

using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using Debug = System.Diagnostics.Trace;
using SB = System.Text.StringBuilder;
using System.Numerics;
using static System.Math;
using Number = System.Int32;

namespace Program {
	public class Solver {
		Random rnd = new Random();
		public void Solve() {
			var n = ri;
			var a = Enumerate(n, x => rl);
			var cnt = new long[60];
			foreach (var x in a)
				for (int i = 0; i < 60; i++) if ((x >> i & 1) == 1) cnt[i]++;
			Debug.WriteLine(cnt.AsJoinedString());
			var ans = 0L;
			const long M = (long)1e9 + 7;
			for (int i = 0; i < 60; i++) {
				var v = 1L << i;
				v = v * cnt[i] % M;
				v = v * (n - cnt[i]) % M;
				Debug.WriteLine(v);
				ans = (ans + v) % M;
			}
			Console.WriteLine(ans);

		}
		const long INF = 1L << 60;
		static int[] dx = { -1, 0, 1, 0 };
		static int[] dy = { 0, 1, 0, -1 };
		int ri { get { return sc.Integer(); } }
		long rl { get { return sc.Long(); } }
		double rd { get { return sc.Double(); } }
		string rs { get { return sc.Scan(); } }
		public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());

		static T[] Enumerate<T>(int n, Func<int, T> f) {
			var a = new T[n];
			for (int i = 0; i < a.Length; ++i) a[i] = f(i);
			return a;
		}
		static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }
	}
}

#region main
static class Ex {
	static public string AsString(this IEnumerable<char> ie) { return new string(ie.ToArray()); }
	static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") {
		return string.Join(st, ie);
	}
	static public void Main() {
		Console.SetOut(new Program.IO.Printer(Console.OpenStandardOutput()) { AutoFlush = true });
		var solver = new Program.Solver();
		/* 
		var t = new System.Threading.Thread(solver.Solve, 50000000);
		t.Start();
		t.Join();
		//*/
		solver.Solve();
		Console.Out.Flush();
	}
}
#endregion
#region Ex
namespace Program.IO {
	using System.IO;
	using System.Text;
	using System.Globalization;

	public class Printer : StreamWriter {
		public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
		public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
	}

	public class StreamScanner {
		public StreamScanner(Stream stream) { str = stream; }

		public readonly Stream str;
		private readonly byte[] buf = new byte[1024];
		private int len, ptr;
		public bool isEof = false;
		public bool IsEndOfStream { get { return isEof; } }

		private byte read() {
			if (isEof) return 0;
			if (ptr >= len) {
				ptr = 0;
				if ((len = str.Read(buf, 0, 1024)) <= 0) {
					isEof = true;
					return 0;
				}
			}
			return buf[ptr++];
		}

		public char Char() {
			byte b = 0;
			do b = read(); while ((b < 33 || 126 < b) && !isEof);
			return (char)b;
		}
		public string Scan() {
			var sb = new StringBuilder();
			for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b);
			return sb.ToString();
		}
		public string ScanLine() {
			var sb = new StringBuilder();
			for (var b = Char(); b != '\n' && b != 0; b = (char)read()) if (b != '\r') sb.Append(b);
			return sb.ToString();
		}
		public long Long() { return isEof ? long.MinValue : long.Parse(Scan()); }
		public int Integer() { return isEof ? int.MinValue : int.Parse(Scan()); }
		public double Double() { return isEof ? double.NaN : double.Parse(Scan(), CultureInfo.InvariantCulture); }
	}
}

#endregion

#region Set
/// <summary>
/// 要素の追加、削除、検索、取得が可能な集合を表します．
/// </summary>
/// <typeparam name="T">優先度付きキュー内の要素の型を指定します．</typeparam>
/// <remarks>内部的にはAVL木によって実装されています．</remarks>
public class Set<T> {
	Node root;
	readonly IComparer<T> comparer;
	readonly Node nil;
	/// <summary>
	/// 多重集合かどうかを表します．
	/// </summary>
	public bool IsMultiSet { get; set; }
	public Set(IComparer<T> comparer) {
		nil = new Node(default(T));
		root = nil;
		this.comparer = comparer;
	}
	public Set(Comparison<T> comaprison) : this(Comparer<T>.Create(comaprison)) { }
	public Set() : this(Comparer<T>.Default) { }
	/// <summary>
	/// 要素をコレクションに追加します．
	/// </summary>
	/// <remarks>この操作は計算量 O(log N) で実行されます．</remarks>
	public bool Add(T v) {
		return insert(ref root, v);
	}
	/// <summary>
	/// v が存在するならコレクションから削除します．
	/// </summary>
	/// <remarks>この操作は計算量 O(log N) で実行されます．</remarks>
	public bool Remove(T v) {
		return remove(ref root, v);
	}
	/// <summary>
	/// 0-indexed で index 番目の要素をコレクションから取得します．．
	/// </summary>
	/// <remarks>この操作は計算量 O(log N) で実行されます．</remarks>
	public T this[int index] { get { return find(root, index); } }
	public int Count { get { return root.Count; } }
	public void RemoveAt(int k) {
		if (k < 0 || k >= root.Count) throw new ArgumentOutOfRangeException();
		removeAt(ref root, k);
	}
	/// <summary>
	/// このコレクションに含まれる要素を昇順に並べて返します．
	/// </summary>
	/// <remarks>この操作は計算量 O(N) で実行されます．</remarks>
	public T[] Items {
		get {
			var ret = new T[root.Count];
			var k = 0;
			walk(root, ret, ref k);
			return ret;
		}
	}
	void walk(Node t, T[] a, ref int k) {
		if (t.Count == 0) return;
		walk(t.lst, a, ref k);
		a[k++] = t.Key;
		walk(t.rst, a, ref k);
	}

	bool insert(ref Node t, T key) {
		if (t.Count == 0) { t = new Node(key); t.lst = t.rst = nil; t.Update(); return true; }
		var cmp = comparer.Compare(t.Key, key);
		bool res;
		if (cmp > 0)
			res = insert(ref t.lst, key);
		else if (cmp == 0) {
			if (IsMultiSet) res = insert(ref t.lst, key);
			else return false;
		}
		else res = insert(ref t.rst, key);
		balance(ref t);
		return res;
	}
	bool remove(ref Node t, T key) {
		if (t.Count == 0) return false;
		var cmp = comparer.Compare(key, t.Key);
		bool ret;
		if (cmp < 0) ret = remove(ref t.lst, key);
		else if (cmp > 0) ret = remove(ref t.rst, key);
		else {
			ret = true;
			var k = t.lst.Count;
			if (k == 0) { t = t.rst; return true; }
			if (t.rst.Count == 0) { t = t.lst; return true; }


			t.Key = find(t.lst, k - 1);
			removeAt(ref t.lst, k - 1);
		}
		balance(ref t);
		return ret;
	}
	void removeAt(ref Node t, int k) {
		var cnt = t.lst.Count;
		if (cnt < k) removeAt(ref t.rst, k - cnt - 1);
		else if (cnt > k) removeAt(ref t.lst, k);
		else {
			if (cnt == 0) { t = t.rst; return; }
			if (t.rst.Count == 0) { t = t.lst; return; }

			t.Key = find(t.lst, k - 1);
			removeAt(ref t.lst, k - 1);
		}
		balance(ref t);
	}
	void balance(ref Node t) {
		var balance = t.lst.Height - t.rst.Height;
		if (balance == -2) {
			if (t.rst.lst.Height - t.rst.rst.Height > 0) { rotR(ref t.rst); }
			rotL(ref t);
		}
		else if (balance == 2) {
			if (t.lst.lst.Height - t.lst.rst.Height < 0) rotL(ref t.lst);
			rotR(ref t);
		}
		else t.Update();
	}

	T find(Node t, int k) {
		if (k < 0 || k > root.Count) throw new ArgumentOutOfRangeException();
		for (; ; )
		{
			if (k == t.lst.Count) return t.Key;
			else if (k < t.lst.Count) t = t.lst;
			else { k -= t.lst.Count + 1; t = t.rst; }
		}
	}
	/// <summary>
	/// コレクションに含まれる要素であって、 v 以上の最小の要素の番号を返します。
	/// </summary>
	/// <remarks>この操作は計算量 O(log N) で実行されます．</remarks>
	public int LowerBound(T v) {
		var k = 0;
		var t = root;
		for (; ; )
		{
			if (t.Count == 0) return k;
			if (comparer.Compare(v, t.Key) <= 0) t = t.lst;
			else { k += t.lst.Count + 1; t = t.rst; }
		}
	}
	/// <summary>
	/// コレクションに含まれる要素であって、 v より真に大きい、最小の要素の番号を返します。
	/// </summary>
	/// <remarks>この操作は計算量 O(log N) で実行されます．</remarks>
	public int UpperBound(T v) {
		var k = 0;
		var t = root;
		for (; ; )
		{
			if (t.Count == 0) return k;
			if (comparer.Compare(t.Key, v) <= 0) { k += t.lst.Count + 1; t = t.rst; }
			else t = t.lst;
		}
	}

	void rotR(ref Node t) {
		var l = t.lst;
		t.lst = l.rst;
		l.rst = t;
		t.Update();
		l.Update();
		t = l;
	}
	void rotL(ref Node t) {
		var r = t.rst;
		t.rst = r.lst;
		r.lst = t;
		t.Update();
		r.Update();
		t = r;
	}


	class Node {
		public Node(T key) {
			Key = key;
		}
		public int Count { get; private set; }
		public sbyte Height { get; private set; }
		public T Key { get; set; }
		public Node lst, rst;
		public void Update() {
			Count = 1 + lst.Count + rst.Count;
			Height = (sbyte)(1 + Math.Max(lst.Height, rst.Height));
		}
		public override string ToString() {
			return string.Format("Count = {0}, Key = {1}", Count, Key);
		}
	}
}
#endregion