Problem p02651 (python)

Fixed submission:
def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    s = list(input())
    basis = []
    for i in range(n-1, -1, -1):
        for v in basis:
            if a[i] > (a[i] ^ v):
                a[i] = a[i] ^ v
        if a[i] > 0:
            if s[i] == '0':
                basis.append(a[i])
            else:
                return 1
    return 0


t = int(input())
for _ in range(t):
    print(solve())

Buggy submission (verdict: Runtime Error):
def inv(a, mod):
    r = [1, 0, a]
    w = [0, 1, mod]
    while w[2] != 1:
        q = r[2]//w[2]
        r_new = [r[0]-q*w[0], r[1]-q*w[1], r[2]-q*w[2]]
        r = w
        w = r_new
    x, y = w[0], w[1]    # a*x+y*mod = 1
    return (mod+x % mod) % mod


def gauss_jordan_mod(A, mod):
    EPS = 1e-8
    n = len(A)
    m = len(A[0])
    for i in range(n):
        if len(A[i]) != m:
            return "Error!"

    for i in range(n):
        for j in range(m):
            A[i][j] %= mod

    rank = 0
    for i in range(n):
        pivot = i
        for k in range(i, n):
            if abs(A[k][i]) > abs(A[pivot][i]):
                pivot = k
        A[i], A[pivot] = A[pivot], A[i]
        if abs(A[i][i]) < EPS:
            continue
        rank += 1
        s = inv(A[i][i], mod)
        for j in range(i, m):
            A[i][j] *= s
            A[i][j] %= mod
        for k in range(n):
            if k != i:
                s = A[k][i]
                for j in range(i, m):
                    A[k][j] -= s * A[i][j]
                    A[k][j] %= mod
    return A, rank


def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    s = input()

    A = []
    rank = 0
    for i in range(n-1, -1, -1):
        cur = []
        for k in range(62):
            cur.append(a[i] >> k & 1)

        if s[i] == '1':
            B, rank_nxt = gauss_jordan_mod(A + [cur], 2)
            if rank_nxt > rank:
                return 1
        else:
            A, rank = gauss_jordan_mod(A + [cur], 2)
    return 0


t = int(input())
for _ in range(t):
    print(solve())

Buggy submission (verdict: Runtime Error):
def inv(a, mod):
    r = [1, 0, a]
    w = [0, 1, mod]
    while w[2] != 1:
        q = r[2]//w[2]
        r_new = [r[0]-q*w[0], r[1]-q*w[1], r[2]-q*w[2]]
        r = w
        w = r_new
    x, y = w[0], w[1]    # a*x+y*mod = 1
    return (mod+x % mod) % mod


def gauss_jordan_mod(A, mod):
    EPS = 1e-8
    n = len(A)
    m = len(A[0])
    for i in range(n):
        if len(A[i]) != m:
            return "Error!"

    for i in range(n):
        for j in range(m):
            A[i][j] %= mod

    rank = 0
    for i in range(n):
        pivot = i
        for k in range(i, n):
            if abs(A[k][i]) > abs(A[pivot][i]):
                pivot = k
        A[i], A[pivot] = A[pivot], A[i]
        if abs(A[i][i]) < EPS:
            continue
        rank += 1
        s = inv(A[i][i], mod)
        for j in range(i, m):
            A[i][j] *= s
            A[i][j] %= mod
        for k in range(n):
            if k != i:
                s = A[k][i]
                for j in range(i, m):
                    A[k][j] -= s * A[i][j]
                    A[k][j] %= mod
    return A, rank


def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    s = input()

    A = [[0 for _ in range(62)]]
    rank = 0
    for i in range(n-1, -1, -1):
        cur = []
        for k in range(62):
            cur.append(a[i] >> k & 1)

        if s[i] == '1':
            B, rank_nxt = gauss_jordan_mod(A + [cur], 2)
            if rank_nxt > rank:
                return 1
        else:
            A, rank = gauss_jordan_mod(A + [cur], 2)
    return 0


t = int(input())
for _ in range(t):
    print(solve())