Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]


N = int(input())

uf = UnionFind(N)

xy = []
min_y = []
for i in range(N):
    x, y = map(int, input().split())
    xy.append((x, y, i))
    min_y.append(y)

xy.sort()

s = set()

for i in range(N):
    x, y, a = xy[i]
    s.add(i)

    remove = set()
    for j in s:
        if j == i:
            continue

        x2, y2, b = xy[j]

        if y2 < y:
            uf.union(b, a)
            root = uf.find(b)
            if min_y[root] > y:
                min_y[root] = y
                remove.add(j)
            else:
                remove.add(i)
    s -= remove

for i in range(N):
    print(uf.get_size(i))


Fixed submission:
class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]


N = int(input())

uf = UnionFind(N)

xy = []
min_y = []
for i in range(N):
    x, y = map(int, input().split())
    xy.append((x, y, i))
    min_y.append(y)

xy.sort()

min_y = N+1

for i in range(N-1):
    x, y, a = xy[i]
    x1, y1, b = xy[i+1]
    min_y = min(min_y, y)
    if min_y != N - i:
        uf.union(a, b)

for i in range(N):
    print(uf.get_size(i))
Buggy python submission (verdict: Runtime Error):
from collections import Counter


# 拡張ユークリッド互除法
# gcd(a,b) と ax + by = gcd(a,b) の最小整数解を返す
def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y


def chineseRem(b1, m1, b2, m2):
    # 中国剰余定理
    # x ≡ b1 (mod m1) ∧ x ≡ b2 (mod m2) <=> x ≡ r (mod m)
    # となる(r. m)を返す
    # 解無しのとき(0, -1)
    d, p, q = egcd(m1, m2)
    if (b2 - b1) % d != 0:
        return 0, -1
    m = m1 * (m2 // d)  # m = lcm(m1, m2)
    tmp = (b2-b1) // d * p % (m2 // d)
    r = (b1 + m1 * tmp) % m
    return r, m


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


N = int(input())
p = prime_factorize(2*N)
c = Counter(p)

if N == 1:
    print(２)

if len(c.keys()) == 1:
    print(N-1)
    exit()

d = [k ** v for k, v in c.items()]

m = len(d)

ans = N-1
for i in range(2 ** m):
    a, b = 1, 1
    for j in range(m):
        if (i >> j) & 1:
            a *= d[j]
        else:
            b *= d[j]

    if a == 1 or b == 1:
        continue

    ans = min(ans, chineseRem(0, a, b-1, b)[0])
    ans = min(ans, chineseRem(a-1, a, 0, b)[0])

print(ans)


Fixed submission:
from collections import Counter


# 拡張ユークリッド互除法
# gcd(a,b) と ax + by = gcd(a,b) の最小整数解を返す
def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y


def chineseRem(b1, m1, b2, m2):
    # 中国剰余定理
    # x ≡ b1 (mod m1) ∧ x ≡ b2 (mod m2) <=> x ≡ r (mod m)
    # となる(r. m)を返す
    # 解無しのとき(0, -1)
    d, p, q = egcd(m1, m2)
    if (b2 - b1) % d != 0:
        return 0, -1
    m = m1 * (m2 // d)  # m = lcm(m1, m2)
    tmp = (b2-b1) // d * p % (m2 // d)
    r = (b1 + m1 * tmp) % m
    return r, m


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


N = int(input())
p = prime_factorize(2*N)
c = Counter(p)

if N == 1:
    print(1)
    exit()

if len(c.keys()) == 1:
    print(N-1)
    exit()

d = [k ** v for k, v in c.items()]

m = len(d)

ans = N-1
for i in range(2 ** m):
    a, b = 1, 1
    for j in range(m):
        if (i >> j) & 1:
            a *= d[j]
        else:
            b *= d[j]

    if a == 1 or b == 1:
        continue

    ans = min(ans, chineseRem(0, a, b-1, b)[0])
    ans = min(ans, chineseRem(a-1, a, 0, b)[0])

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
P = [int(p) - 1 for p in input().split()]
C = list(map(int, input().split()))

max_score = -10**9
for i in range(N):
    memo = [P[i]]
    score = C[P[i]]
    move = 1
    max_score = max(score, max_score)

    while move < K and P[memo[-1]] not in memo:
        memo.append(P[memo[-1]])
        score += C[memo[-1]]
        move += 1
        max_score = max(score, max_score)

    if move < K and score > 0:
        score = (K // move) * score
        move *= (K // move)
        max_score = max(score, max_score)

        cur = i
        while move < K:
            cur = P[cur]
            score += C[cur]
            move += 1
            max_score = max(score, max_score)

print(max_score)

Fixed submission:
N, K = map(int, input().split())
P = [int(p) - 1 for p in input().split()]
C = list(map(int, input().split()))

max_score = -10 ** 9
for i in range(N):
    memo = set()
    score = 0
    cur = i
    move = 0

    while move < K and P[cur] not in memo:
        cur = P[cur]
        memo.add(cur)
        score += C[cur]
        move += 1
        max_score = max(score, max_score)

    if move < K and score > 0:
        score = max(K // move - 1, 1) * score
        move *= max(K // move - 1, 1)
        max_score = max(score, max_score)

        cur = i
        while move < K:
            cur = P[cur]
            score += C[cur]
            move += 1
            max_score = max(score, max_score)

print(max_score)
Buggy python submission (verdict: Time Limit Exceeded):
K = int(input())

if K % 2 == 0:
    print(-1)
else:
    i = 1
    v = 7
    while True:
        if v % K == 0:
            print(i)
            break
        else:
            i += 1
            v = v * 10 + 7
            v %= K

Fixed submission:
K = int(input())

if K % 2 == 0 or K % 5 == 0:
    print(-1)
else:
    i = 1
    v = 7
    while True:
        if v % K == 0:
            print(i)
            break
        else:
            i += 1
            v = v * 10 + 7
            v %= K
Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush, heappop

n, m = map(int, input().split())

neighbor = [[] for _ in range(n+1)]

for i in range(m):
    A, B = map(int, input().split())
    neighbor[A].append(B)
    neighbor[B].append(A)

h = []

heappush(h, (0, 1, 0))

ans = [0] * (n+1)
while h:
    step, cur, prev = heappop(h)
    if ans[cur] != 0:
        continue

    ans[cur] = prev

    for i in neighbor[cur]:
        heappush(h, (step+1, i, cur))

print('Yes')
for i in range(2, n+1):
    print(ans[i])


Fixed submission:
from heapq import heappush, heappop
import sys
input = sys.stdin.buffer.readline

n, m = map(int, input().split())

neighbor = [[] for _ in range(n+1)]

for i in range(m):
    A, B = map(int, input().split())
    neighbor[A].append(B)
    neighbor[B].append(A)

h = []

heappush(h, (0, 1, 0))

ans = [0] * (n+1)
while h:
    step, cur, prev = heappop(h)

    for i in neighbor[cur]:
        if ans[i] == 0:
            ans[i] = cur
            heappush(h, (step+1, i, cur))

print('Yes')
for i in range(2, n+1):
    print(ans[i])
Buggy python submission (verdict: Runtime Error):
n, m, k = map(int, input().split())


class Combination:
    """
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv


mod = 998244353
total = 0
comb = Combination(n-1, mod)

v2 = [0] * (k+1)
v2[k] = (m * pow(m-1, n-k-1, mod)) % mod
for i in range(k-1, -1, -1):
    v2[i] = (v2[i+1] * (m-1)) % mod

for i in range(k+1):
    v = comb(n-1, i)
    total += (v * v2[i]) % mod
    total %= mod

print(total)

Fixed submission:
n, m, k = map(int, input().split())


class Combination:
    """
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv


mod = 998244353
total = 0
comb = Combination(n, mod)

v2 = [0] * (k+1)
v2[k] = (m * pow(m-1, n-k-1, mod)) % mod
for i in range(k-1, -1, -1):
    v2[i] = (v2[i+1] * (m-1)) % mod

for i in range(k+1):
    v = comb(n-1, i)
    total += (v * v2[i]) % mod
    total %= mod

print(total)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

s = deque(input().rstrip())
q = int(input())
for i in range(q):
    query = input()
    if int(query[0]) == 1:
        s.reverse()
    else:
        _, f, c = query.split()
        if int(f) == 1:
            s.appendleft(c)
        else:
            s.append(c)

print(''.join(s))

Fixed submission:
from collections import deque

s = deque(input().rstrip())
q = int(input())

reverse = False
for i in range(q):
    query = input()
    if int(query[0]) == 1:
        reverse = not reverse
    else:
        _, f, c = query.split()
        if int(f) == 1:
            if reverse:
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if reverse:
                s.appendleft(c)
            else:
                s.append(c)
if reverse:
    s.reverse()

print(''.join(s))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

n, p = map(int, input().split())
s = input().rstrip()

total = 0
if p in [2, 5]:
    for i in range(n-1, -1, -1):
        if int(s[i]) % p == 0:
            total += i + 1
else:
    dp = [0] * (n + 1)
    fac = 1
    for i in range(n-1, -1, -1):
        dp[i] = (dp[i + 1] + int(s[i]) * fac) % p
        fac *= 10 % p

    c = Counter(dp)

    for k, v in c.items():
        if v >= 2:
            total += v * (v - 1) // 2

print(total)


Fixed submission:
from collections import Counter

n, p = map(int, input().split())
s = input().rstrip()

total = 0
if p in [2, 5]:
    for i in range(n-1, -1, -1):
        if int(s[i]) % p == 0:
            total += i + 1
else:
    dp = [0] * (n + 1)
    fac = 1
    for i in range(n-1, -1, -1):
        dp[i] = (dp[i + 1] + int(s[i]) * fac) % p
        fac = (fac * 10) % p

    c = Counter(dp)

    for k, v in c.items():
        if v >= 2:
            total += v * (v - 1) // 2

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

n, p = map(int, input().split())
s = input().rstrip()

total = 0
if p in [2, 5]:
    for i in range(n-1, -1, -1):
        if int(s[i]) % p == 0:
            total += i + 1
else:
    dp = [0] * (n + 1)
    fac = 1
    for i in range(n-1, -1, -1):
        dp[i] = (dp[i + 1] + int(s[i]) * fac) % p
        fac *= 10

    c = Counter(dp)

    for k, v in c.items():
        if v >= 2:
            total += v * (v - 1) // 2

print(total)


Fixed submission:
from collections import Counter

n, p = map(int, input().split())
s = input().rstrip()

total = 0
if p in [2, 5]:
    for i in range(n-1, -1, -1):
        if int(s[i]) % p == 0:
            total += i + 1
else:
    dp = [0] * (n + 1)
    fac = 1
    for i in range(n-1, -1, -1):
        dp[i] = (dp[i + 1] + int(s[i]) * fac) % p
        fac = (fac * 10) % p

    c = Counter(dp)

    for k, v in c.items():
        if v >= 2:
            total += v * (v - 1) // 2

print(total)
Buggy python submission (verdict: Time Limit Exceeded):
n, m, k = map(int, input().split())

friend = [[] for _ in range(n + 1)]
candidate = [[] for _ in range(n + 1)]


def append_candidate(a, b):
    if b not in candidate[a] and b not in friend[a] and b != a:
        candidate[a].append(b)
    if a not in candidate[b] and a not in friend[b] and a != b:
        candidate[b].append(a)


def update_candidate(a, b):
    for af in friend[a]:
        append_candidate(af, b)
        for bf in friend[b]:
            append_candidate(af, bf)
        for bc in candidate[b]:
            append_candidate(af, bc)

    for ac in candidate[a]:
        append_candidate(ac, b)
        for bf in friend[b]:
            append_candidate(ac, bf)
        for bc in candidate[b]:
            append_candidate(ac, bc)


for i in range(m):
    a, b = map(int, input().split())

    if a in candidate[b]:
        candidate[b].remove(a)
    if b in candidate[a]:
        candidate[a].remove(b)

    friend[a].append(b)
    friend[b].append(a)

    update_candidate(a, b)
    update_candidate(b, a)

for i in range(k):
    c, d = map(int, input().split())
    if c in candidate[d]:
        candidate[d].remove(c)
    if d in candidate[c]:
        candidate[c].remove(d)

print(' '.join(str(len(candidate[i])) for i in range(1, n+1)))


Fixed submission:
class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]


n, m, k = map(int, input().split())

friend = [[] for _ in range(n)]
block = [[] for _ in range(n)]

uf = UnionFind(n)

for i in range(m):
    a, b = map(int, input().split())
    friend[a-1].append(b-1)
    friend[b-1].append(a-1)

    uf.union(a-1, b-1)

for i in range(k):
    c, d = map(int, input().split())
    block[c-1].append(d-1)
    block[d-1].append(c-1)

candidate = [0] * n

for i in range(n):
    candidate[i] = uf.get_size(i) - len(friend[i]) - 1
    for j in block[i]:
        if uf.is_same(i, j):
            candidate[i] -= 1

print(' '.join(list(map(str, candidate))))
Buggy python submission (verdict: Time Limit Exceeded):
h, n = map(int, input().split())
a = []
b = []
for i in range(n):
    a_, b_ = map(int, input().split())
    a.append(a_)
    b.append(b_)

dp = [10**9] * (h+1)
dp[0] = 0

for i in range(1, h+1):
    for j in range(n):
        dp[i] = min(dp[i], dp[max(i-a[j], 0)] + b[j])

print(dp[-1])

Fixed submission:
h, n = map(int, input().split())
a = []
b = []
for i in range(n):
    a_, b_ = map(int, input().split())
    a.append(a_)
    b.append(b_)

dp = [10**9] * (h+1)
dp[0] = 0

for i in range(1, h+1):
    for j in range(n):
        dp[i] = min(dp[i], dp[max(i-a[j], 0)] + b[j])

print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
import queue

def solve(maze, start):
    if maze[start[0]][start[1]] == '#':
        return 0

    q = queue.Queue()
    q.put((start, 0))

    h = len(maze)
    w = len(maze[0])

    memo = [[False] * w for _ in range(h)]

    ans = 0

    while not q.empty():
        p, i = q.get()

        if not memo[p[0]][p[1]]:
            memo[p[0]][p[1]] = True
            ans = i
        else:
            continue

        if p[0] < h-1 and maze[p[0] + 1][p[1]] == '.':
            q.put(((p[0] + 1, p[1]), i + 1))

        if p[0] > 0 and maze[p[0] - 1][p[1]] == '.':
            q.put(((p[0] - 1, p[1]), i + 1))

        if p[1] < w-1 and maze[p[0]][p[1] + 1] == '.':
            q.put(((p[0], p[1] + 1), i + 1))

        if p[1] > 0 and maze[p[0]][p[1] - 1] == '.':
            q.put(((p[0], p[1] - 1), i + 1))

    return ans


h, w = map(int, input().split())

maze = []
for i in range(h):
    s = list(input())
    maze.append(s)

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, solve(maze, (i, j)))

print(ans)


Fixed submission:
import queue

def solve(maze, start):
    if maze[start[0]][start[1]] == '#':
        return 0

    q = queue.Queue()
    q.put((start, 0))

    h = len(maze)
    w = len(maze[0])

    memo = [[False] * w for _ in range(h)]
    memo[start[0]][start[1]] = True

    ans = 0

    while not q.empty():
        p, i = q.get()
        ans = i

        if p[0] < h-1 and not memo[p[0] + 1][p[1]] and maze[p[0] + 1][p[1]] == '.':
            q.put(((p[0] + 1, p[1]), i + 1))
            memo[p[0] + 1][p[1]] = True

        if p[0] > 0 and not memo[p[0] - 1][p[1]] and maze[p[0] - 1][p[1]] == '.':
            q.put(((p[0] - 1, p[1]), i + 1))
            memo[p[0] - 1][p[1]] = True

        if p[1] < w-1 and not memo[p[0]][p[1] + 1] and maze[p[0]][p[1] + 1] == '.':
            q.put(((p[0], p[1] + 1), i + 1))
            memo[p[0]][p[1] + 1] = True

        if p[1] > 0 and not memo[p[0]][p[1] - 1] and maze[p[0]][p[1] - 1] == '.':
            q.put(((p[0], p[1] - 1), i + 1))
            memo[p[0]][p[1] - 1] = True

    return ans


h, w = map(int, input().split())

maze = []
for i in range(h):
    s = list(input())
    maze.append(s)

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, solve(maze, (i, j)))

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math
import operator as op
from functools import reduce


def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer // denom


n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))

total = 0

for i in range(n-m+1):
    p = n - i - 1
    q = m - 1

    c = ncr(p, q)

    total -= c * a[i]
    total += c * a[-1-i]

print(total % (10**9 + 7))


Fixed submission:
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
mod = 10**9 + 7

f = [1, 1]
for i in range(2, n):
    f.append((f[i-1] * i) % mod)

total = 0
for i in range(n - k + 1):
    p = n - i - 1
    q = k - 1

    c = f[p] * pow(f[p-q], mod-2, mod) * pow(f[q], mod-2, mod) % mod
    total += c * (a[-1-i] - a[i])
    total = total % mod

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
import math

n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))

total = 0

for i in range(n-m+1):
    p = n - i - 1
    q = m - 1

    c = math.factorial(p) // math.factorial(p-q) // math.factorial(q)

    total -= c * a[i]
    total += c * a[-1-i]

print(total % (10**9 + 7))


Fixed submission:
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
mod = 10**9 + 7

f = [1, 1]
for i in range(2, n):
    f.append((f[i-1] * i) % mod)

total = 0
for i in range(n - k + 1):
    p = n - i - 1
    q = k - 1

    c = f[p] * pow(f[p-q], mod-2, mod) * pow(f[q], mod-2, mod) % mod
    total += c * (a[-1-i] - a[i])
    total = total % mod

print(total)

Buggy python submission (verdict: Runtime Error):
import math

n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))

total = 0

for i in range(n-m+1):
    p = n - i - 1
    q = m - 1

    c = int(math.factorial(p) / math.factorial(p-q) / math.factorial(q))

    total -= c * a[i]
    total += c * a[-1-i]

print(total % (10**9 + 7))


Fixed submission:
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
mod = 10**9 + 7

f = [1, 1]
for i in range(2, n):
    f.append((f[i-1] * i) % mod)

total = 0
for i in range(n - k + 1):
    p = n - i - 1
    q = k - 1

    c = f[p] * pow(f[p-q], mod-2, mod) * pow(f[q], mod-2, mod) % mod
    total += c * (a[-1-i] - a[i])
    total = total % mod

print(total)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

bits = [0] * 60
for d in a:
    for i in range(60):
        if d % 2 == 1:
            bits[i] += 1
        d >>= 1
        if d == 0:
            break

total = 0
mod = 10 ** 9 + 7
for i in range(60):
    total += (2 ** i) * (bits[i] * (n - bits[i]))
    total %= mod
print(total)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))

bits = [0] * 60
for d in a:
    for i in range(60):
        if d % 2 == 1:
            bits[i] += 1
        d >>= 1
        if d == 0:
            break

total = 0
mod = 10 ** 9 + 7
for i in range(60):
    total += (2 ** i) * (bits[i] * (n - bits[i]))
    total %= mod
print(total)
Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n = int(input())
l = sorted(list(map(int, input().split())))

total = 0
for i in range(n-1):
    for j in range(i+1, n):
        l2 = l[j+1:]
        total += bisect.bisect_left(l2, l[j]+l[i]) - bisect.bisect_right(l2, l[j]-l[i])
print(total)

Fixed submission:
import bisect

n = int(input())
l = sorted(list(map(int, input().split())))

total = 0
for i in range(n-1):
    for j in range(i+1, n):
        l2 = l[j+1:]
        total += bisect.bisect_left(l2, l[j]+l[i])
print(total)