Problem p02973 (python)

Fixed submission:
from bisect import bisect_right

N = int(input())
A = [int(input()) for _ in range(N)]
A.reverse()

L = [A[0]]
for a in A[1:]:
    if a >= L[-1]:
        L.append(a)
    else:
        L[bisect_right(L, a)] = a

print(len(L))

Buggy submission (verdict: Runtime Error):
mod = 10 ** 9 + 7

N, K = map(int, input().split())

dp = [[[0 for _ in range(2610)] for _ in range(51)] for _ in range(51)]
dp[0][0][0] = 1

for i in range(N):
    for j in range(N):
        for k in range(K + 1):
            dp[i + 1][j + 1][k + 2 * j + 2] += dp[i][j][k]
            if j > 0:
                dp[i + 1][j][k + 2 * j] += dp[i][j][k] * j
                dp[i + 1][j][k + 2 * j] += dp[i][j][k] * j

            dp[i + 1][j][k + 2 * j] += dp[i][j][k]

            if j > 0:
                dp[i + 1][j - 1][k + 2 * j - 2] += dp[i][j][k] * j * j


print(dp[N][0][K] % mod)

Buggy submission (verdict: Runtime Error):
class BIT:
    def __init__(self, N):
        self.size = N
        self.tree = [0] * (N + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    def __str__(self):
        return "BIT: [{}]".format(
            ", ".join(str(self.sum(i + 1) - self.sum(i))
                      for i in range(self.size))
        )

    def bisect_left(self, r):
        bit = self.tree

        if r == 0:
            return 0

        l = 1 << (r.bit_length() - 1)
        if r == l:
            l //= 2

        k = l // 2
        while r - l >= 1:
            if bit[r] > bit[l]:
                l += max(1, k)
            else:
                r = l
                l //= 2
            k //= 2

        return r if bit[r] > bit[l] else l


N, *A = map(int, open(0))

memo = {a: i for i, a in enumerate(sorted(set(A)), 1)}
B = [memo[a] for a in A]
M = len(memo) + 10
bit = BIT(M)
for b in B:
    i = bit.bisect_left(b - 1)
    if i != 0:
        bit.add(i, -1)
    bit.add(b, 1)

print(bit.sum(M))

Buggy submission (verdict: Runtime Error):
class BIT:
    def __init__(self, N):
        self.size = N
        self.tree = [0] * (N + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    def __str__(self):
        return "BIT: [{}]".format(
            ", ".join(str(self.sum(i + 1) - self.sum(i))
                      for i in range(self.size))
        )

    def bisect_left(self, r):
        bit = self.tree

        if r == 0:
            return 0

        l = 1 << (r.bit_length() - 1)
        if r == l:
            l //= 2

        k = l // 2
        while r - l >= 1:
            if bit[r] > bit[l]:
                l += max(1, k)
            else:
                r = l
                l //= 2
            k //= 2

        return r if bit[r] > bit[l] else l


N, *A = map(int, open(0))

memo = {a: i for i, a in enumerate(sorted(set(A)), 1)}
B = [memo[a] for a in A]
M = len(memo)
bit = BIT(M)
for b in B:
    i = bit.bisect_left(b - 1)
    if i != 0:
        bit.add(i, -1)
    bit.add(b, 1)

print(bit.sum(M))