Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
from decimal import Decimal
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
a.sort()
lmx = len(bin(a[-1])) - 2
cnt = [[0,0] for _ in range(lmx)]
for i in range(n):
    tmp = bin(a[i])[::-1]
    ln = len(tmp)-2
    for j in range(lmx):
        tt = 0
        if j < ln:
            tt = int(tmp[j])
        cnt[j][tt] += 1
# print(cnt)
res = 0
for i,bit in enumerate(cnt):
    x,y = bit
    res += x*y*pow(2,i)
    res %= mod
print(res)

Fixed submission:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
a.sort()
ln = len(bin(a[-1])) - 2
bit = [defaultdict(int) for i in range(ln)]
res = 0
for i in a:
    tmp = bin(i)
    for _,j in enumerate(tmp[:1:-1]):
        # print(_,j)
        bit[_][j] += 1
# pprint.pprint(bit)
for i,j in enumerate(bit):
    x = j['0']
    y = j['1']
    if x+y < n:
        x = n - y
    # print(i,x,y)
    c = pow(2,i)
    res += x * y * c
    res %= mod
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
from decimal import Decimal
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = sorted(inpl())
lmx = len(bin(a[-1])) - 2
cnt = [[0,0] for _ in range(lmx)]
for i in range(n):
    tmp = bin(a[i])[::-1]
    ln = len(tmp)-2
    for j in range(lmx):
        tt = 0
        if j < ln:
            tt = int(tmp[j])
        cnt[j][tt] += 1
# print(cnt)
res = 0
for i,bit in enumerate(cnt):
    x,y = bit
    res += x*y*pow(2,i)
    res %= mod
print(res)

Fixed submission:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
a.sort()
ln = len(bin(a[-1])) - 2
bit = [defaultdict(int) for i in range(ln)]
res = 0
for i in a:
    tmp = bin(i)
    for _,j in enumerate(tmp[:1:-1]):
        # print(_,j)
        bit[_][j] += 1
# pprint.pprint(bit)
for i,j in enumerate(bit):
    x = j['0']
    y = j['1']
    if x+y < n:
        x = n - y
    # print(i,x,y)
    c = pow(2,i)
    res += x * y * c
    res %= mod
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
from decimal import Decimal
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
lmx = 0
for i in range(n):
    tmp = bin(a[i])
    a[i] = tmp[2:]
    a[i] = a[i][::-1]
    lmx = max(lmx, len(tmp)-2)
res = 0
cnt = [[0,0] for _ in range(lmx)]
for i in range(n):
    bi = a[i]
    ln = len(bi)
    for j in range(lmx):
        if j < ln:
            cnt[j][int(bi[j])] += 1
        else:
            cnt[j][0] += 1
for i,bit in enumerate(cnt):
    x,y = bit
    res += x*y*pow(2,i)
    res %= mod
print(res)

Fixed submission:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
a.sort()
ln = len(bin(a[-1])) - 2
bit = [defaultdict(int) for i in range(ln)]
res = 0
for i in a:
    tmp = bin(i)
    for _,j in enumerate(tmp[:1:-1]):
        # print(_,j)
        bit[_][j] += 1
# pprint.pprint(bit)
for i,j in enumerate(bit):
    x = j['0']
    y = j['1']
    if x+y < n:
        x = n - y
    # print(i,x,y)
    c = pow(2,i)
    res += x * y * c
    res %= mod
print(res)