Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
import collections
import bisect
import copy
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    a = na()
    bit = [[0] * 20 for _ in range(n)]
    bitidx = [[] * 20 for _ in range(20)]

    for i, ai in enumerate(a):
        cnt = 0
        while ai > 0:
            if ai & 1:
                bit[i][cnt] = 1
                bitidx[cnt].append(i)
            cnt += 1
            ai >>= 1

    ans = n

    for i in range(n):
        min_idx = n
        for j in range(20):
            # jbit目でiの次に1になるidxを探す　ない場合はlen(bitidx[j])が返ってくる
            tmp = bisect.bisect_left(bitidx[j], i + 1)
            if bit[i][j]:
                # 1のときは次の1を探す
                if tmp < len(bitidx[j]):
                    tmp = bitidx[j][tmp]
                else:
                    tmp = n
            else:
                # 0のときは2つ目の1を探す
                tmp += 1
                if tmp < len(bitidx[j]):
                    tmp = bitidx[j][tmp]
                else:
                    tmp = n
            min_idx = min(min_idx, tmp)
        ans += min_idx - i - 1

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
import math
import collections
import bisect
import copy
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    a = na()
    bit = [[0] * 20 for _ in range(n)]
    bitidx = [[] * 20 for _ in range(20)]

    for i, ai in enumerate(a):
        cnt = 0
        while ai > 0:
            if ai & 1:
                bit[i][cnt] = 1
                bitidx[cnt].append(i)
            cnt += 1
            ai >>= 1

    ans = n

    for i in range(n):
        min_idx = n
        for j in range(20):
            # jbit目でiの次に1になるidxを探す　ない場合はlen(bitidx[j])が返ってくる
            tmp = bisect.bisect_left(bitidx[j], i + 1)
            if bit[i][j]:
                # 1のときは次の1を探す
                if tmp < len(bitidx[j]):
                    tmp = bitidx[j][tmp]
                else:
                    tmp = n
            else:
                # 0のときは2つ目の1を探す
                tmp += 1
                if tmp < len(bitidx[j]):
                    tmp = bitidx[j][tmp]
                else:
                    tmp = n
            min_idx = min(min_idx, tmp)
        ans += min_idx - i - 1

    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
import collections
import bisect
import copy
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    a = na()
    bit = [[0] * 20 for _ in range(n)]

    for i, ai in enumerate(a):
        cnt = 0
        while ai > 0:
            bit[i][cnt] = ai & 1
            cnt += 1
            ai >>= 1

    ans = n
    l = 0
    while l < n - 1:
        base = bit[l]
        for r in range(l + 1, n):
            flg = True
            for idx in range(20):
                if base[idx] and bit[r][idx]:
                    flg = False
                    break
                elif base[idx] or bit[r][idx]:
                    base[idx] = 1
            if not flg:
                ans += r - l - 1
                break
            if flg and r == n - 1:
                ans += r - l
                break

        l += 1

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
import math
import collections
import bisect
import copy
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    a = na()
    bit = [[0] * 20 for _ in range(n)]
    bitidx = [[] * 20 for _ in range(20)]

    for i, ai in enumerate(a):
        cnt = 0
        while ai > 0:
            if ai & 1:
                bit[i][cnt] = 1
                bitidx[cnt].append(i)
            cnt += 1
            ai >>= 1

    ans = n

    for i in range(n):
        min_idx = n
        for j in range(20):
            # jbit目でiの次に1になるidxを探す　ない場合はlen(bitidx[j])が返ってくる
            tmp = bisect.bisect_left(bitidx[j], i + 1)
            if bit[i][j]:
                # 1のときは次の1を探す
                if tmp < len(bitidx[j]):
                    tmp = bitidx[j][tmp]
                else:
                    tmp = n
            else:
                # 0のときは2つ目の1を探す
                tmp += 1
                if tmp < len(bitidx[j]):
                    tmp = bitidx[j][tmp]
                else:
                    tmp = n
            min_idx = min(min_idx, tmp)
        ans += min_idx - i - 1

    print(ans)


if __name__ == '__main__':
    main()