Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction

# 10進数表記でnのk進数を求めてlength分0埋め
def ternary (n, k, length):
    if n == 0:
        nums = ['0' for i in range(length)]
        nums = ''.join(nums)
        return nums
    nums = ''
    while n:
        n, r = divmod(n, k)
        nums += str(r)
    nums = nums[::-1]
    nums = nums.zfill(length)
    return nums


def main():
    num = int(input())
    data = list(map(int, input().split()))
    mod = 10 ** 9 + 7

    max_length = len(bin(max(data))) - 2
    bit_count = [0 for i in range(max_length)]

    for i in range(num):
        now_bin = ternary(data[i], 2, max_length)
        data[i] = now_bin
        for j in range(max_length):
            if now_bin[j] == "1":
                bit_count[j] += 1

    flg_data = [0 for i in range(max_length)]

    # for i in range(num):
    #     now_bin = data[i]
    #     for j in range(max_length):
    #         if now_bin[j] == "1":
    #             bit_count[j] -= 1
    #             flg_data[j] += (num - i - 1 - bit_count[j])
    #         else:
    #             flg_data[j] += bit_count[j]

    for i in range(max_length):
        flg_data[i] += bit_count[i] * (num - bit_count[i])

    # print(flg_data)
    # print(bit_count)

    ans = 0
    for j in range(max_length):
        pow_num = max_length - 1 - j
        bbb = pow(2, pow_num, mod)

        ans += bbb * flg_data[j]
        ans %= mod

    print(ans)




if __name__ == '__main__':
    main()



Fixed submission:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction

# 10進数表記でnのk進数を求めてlength分0埋め
def ternary (n, k, length):
    if n == 0:
        nums = ['0' for i in range(length)]
        nums = ''.join(nums)
        return nums
    nums = ''
    while n:
        n, r = divmod(n, k)
        nums += str(r)
    nums = nums[::-1]
    nums = nums.zfill(length)
    return nums


def main():
    num = int(input())
    data = list(map(int, input().split()))
    mod = 10 ** 9 + 7

    max_length = len(bin(max(data))) - 2
    bit_count = [0 for i in range(max_length)]

    for i in range(num):
        now_bin = bin(data[i])[2:]
        now_bin = now_bin.zfill(max_length)
        for j in range(max_length):
            if now_bin[j] == "1":
                bit_count[j] += 1

    flg_data = [0 for i in range(max_length)]


    for i in range(max_length):
        flg_data[i] += bit_count[i] * (num - bit_count[i])

    ans = 0
    for j in range(max_length):
        pow_num = max_length - 1 - j
        bbb = pow(2, pow_num, mod)

        ans += bbb * flg_data[j]
        ans %= mod

    print(ans)




if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction

# 10進数表記でnのk進数を求めてlength分0埋め
def ternary (n, k, length):
    if n == 0:
        nums = ['0' for i in range(length)]
        nums = ''.join(nums)
        return nums
    nums = ''
    while n:
        n, r = divmod(n, k)
        nums += str(r)
    nums = nums[::-1]
    nums = nums.zfill(length)
    return nums


def main():
    num = int(input())
    data = list(map(int, input().split()))
    mod = 10 ** 9 + 7

    max_length = len(bin(max(data))) - 2
    bit_count = [0 for i in range(max_length)]

    for i in range(num):
        now_bin = ternary(data[i], 2, max_length)
        data[i] = now_bin
        for j in range(max_length):
            if now_bin[j] == "1":
                bit_count[j] += 1

    flg_data = [0 for i in range(max_length)]

    for i in range(num):
        now_bin = data[i]
        for j in range(max_length):
            if now_bin[j] == "1":
                bit_count[j] -= 1
                flg_data[j] += (num - i - 1 - bit_count[j])
            else:
                flg_data[j] += bit_count[j]

    ans = 0
    for j in range(max_length):
        pow_num = max_length - 1 - j
        bbb = pow(2, pow_num, mod)

        ans += bbb * flg_data[j]
        ans %= mod

    print(ans)




if __name__ == '__main__':
    main()



Fixed submission:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction

# 10進数表記でnのk進数を求めてlength分0埋め
def ternary (n, k, length):
    if n == 0:
        nums = ['0' for i in range(length)]
        nums = ''.join(nums)
        return nums
    nums = ''
    while n:
        n, r = divmod(n, k)
        nums += str(r)
    nums = nums[::-1]
    nums = nums.zfill(length)
    return nums


def main():
    num = int(input())
    data = list(map(int, input().split()))
    mod = 10 ** 9 + 7

    max_length = len(bin(max(data))) - 2
    bit_count = [0 for i in range(max_length)]

    for i in range(num):
        now_bin = bin(data[i])[2:]
        now_bin = now_bin.zfill(max_length)
        for j in range(max_length):
            if now_bin[j] == "1":
                bit_count[j] += 1

    flg_data = [0 for i in range(max_length)]


    for i in range(max_length):
        flg_data[i] += bit_count[i] * (num - bit_count[i])

    ans = 0
    for j in range(max_length):
        pow_num = max_length - 1 - j
        bbb = pow(2, pow_num, mod)

        ans += bbb * flg_data[j]
        ans %= mod

    print(ans)




if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction

# 10進数表記でnのk進数を求めてlength分0埋め
def ternary (n, k, length):
    if n == 0:
        nums = ['0' for i in range(length)]
        nums = ''.join(nums)
        return nums
    nums = ''
    while n:
        n, r = divmod(n, k)
        nums += str(r)
    nums = nums[::-1]
    nums = nums.zfill(length)
    return nums


def main():
    num = int(input())
    data = list(map(int, input().split()))
    mod = 10 ** 9 + 7

    max_length = len(bin(max(data))) - 2
    bit_count = [0 for i in range(max_length)]

    for i in range(num):
        now_bin = ternary(data[i], 2, max_length)
        data[i] = now_bin
        for j in range(max_length):
            if now_bin[j] == "1":
                bit_count[j] += 1

    pow_data = defaultdict(int)

    ans = 0
    for i in range(num):
        now_bin = data[i]
        for j in range(max_length):
            if now_bin[j] == "1":
                bit_count[j] -= 1
                aaa = (num - i - 1 - bit_count[j])
            else:
                aaa = bit_count[j]

            pow_num = max_length - 1 - j
            if pow_num in pow_data:
                bbb = pow_data[pow_num]
            else:
                bbb = pow(2, pow_num, mod)
                pow_data[pow_num] = bbb

            ans += bbb * aaa
            ans %= mod

    print(ans)




if __name__ == '__main__':
    main()



Fixed submission:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction

# 10進数表記でnのk進数を求めてlength分0埋め
def ternary (n, k, length):
    if n == 0:
        nums = ['0' for i in range(length)]
        nums = ''.join(nums)
        return nums
    nums = ''
    while n:
        n, r = divmod(n, k)
        nums += str(r)
    nums = nums[::-1]
    nums = nums.zfill(length)
    return nums


def main():
    num = int(input())
    data = list(map(int, input().split()))
    mod = 10 ** 9 + 7

    max_length = len(bin(max(data))) - 2
    bit_count = [0 for i in range(max_length)]

    for i in range(num):
        now_bin = bin(data[i])[2:]
        now_bin = now_bin.zfill(max_length)
        for j in range(max_length):
            if now_bin[j] == "1":
                bit_count[j] += 1

    flg_data = [0 for i in range(max_length)]


    for i in range(max_length):
        flg_data[i] += bit_count[i] * (num - bit_count[i])

    ans = 0
    for j in range(max_length):
        pow_num = max_length - 1 - j
        bbb = pow(2, pow_num, mod)

        ans += bbb * flg_data[j]
        ans %= mod

    print(ans)




if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction

# 10進数表記でnのk進数を求めてlength分0埋め
def ternary (n, k, length):
    if n == 0:
        nums = ['0' for i in range(length)]
        nums = ''.join(nums)
        return nums
    nums = ''
    while n:
        n, r = divmod(n, k)
        nums += str(r)
    nums = nums[::-1]
    nums = nums.zfill(length)
    return nums


def main():
    num = int(input())
    data = list(map(int, input().split()))
    mod = 10 ** 9 + 7

    max_length = 61
    bit_count = [0 for i in range(max_length)]

    for i in range(num):
        now_bin = ternary(data[i], 2, max_length)
        # print(now_bin)
        for j in range(max_length):
            if now_bin[j] == "1":
                bit_count[j] += 1


    ans = 0
    for i in range(num):
        now_bin = ternary(data[i], 2, max_length)
        for j in range(max_length):
            if now_bin[j] == "1":
                bit_count[j] -= 1
                ans += pow(2, max_length - 1 - j, mod) * (num - i - 1 - bit_count[j])
            else:
                ans += pow(2, max_length - 1 - j, mod) * bit_count[j]
            ans %= mod

    print(ans)




if __name__ == '__main__':
    main()



Fixed submission:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction

# 10進数表記でnのk進数を求めてlength分0埋め
def ternary (n, k, length):
    if n == 0:
        nums = ['0' for i in range(length)]
        nums = ''.join(nums)
        return nums
    nums = ''
    while n:
        n, r = divmod(n, k)
        nums += str(r)
    nums = nums[::-1]
    nums = nums.zfill(length)
    return nums


def main():
    num = int(input())
    data = list(map(int, input().split()))
    mod = 10 ** 9 + 7

    max_length = len(bin(max(data))) - 2
    bit_count = [0 for i in range(max_length)]

    for i in range(num):
        now_bin = bin(data[i])[2:]
        now_bin = now_bin.zfill(max_length)
        for j in range(max_length):
            if now_bin[j] == "1":
                bit_count[j] += 1

    flg_data = [0 for i in range(max_length)]


    for i in range(max_length):
        flg_data[i] += bit_count[i] * (num - bit_count[i])

    ans = 0
    for j in range(max_length):
        pow_num = max_length - 1 - j
        bbb = pow(2, pow_num, mod)

        ans += bbb * flg_data[j]
        ans %= mod

    print(ans)




if __name__ == '__main__':
    main()