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

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


class ModInt:
    def __init__(self, x):
        self.x = x % MOD

    def __str__(self):
        return str(self.x)

    __repr__ = __str__

    def __add__(self, other):
        return ModInt(self.x + other.x) if isinstance(other, ModInt) else ModInt(self.x + other)

    def __sub__(self, other):
        return ModInt(self.x - other.x) if isinstance(other, ModInt) else ModInt(self.x - other)

    def __mul__(self, other):
        return ModInt(self.x * other.x) if isinstance(other, ModInt) else ModInt(self.x * other)

    def __truediv__(self, other):
        return (
            ModInt(self.x * pow(other.x, MOD - 2, MOD))
            if isinstance(other, ModInt)
            else ModInt(self.x * pow(other, MOD - 2, MOD))
        )

    def __pow__(self, other):
        return (
            ModInt(pow(self.x, other.x, MOD))
            if isinstance(other, ModInt)
            else ModInt(pow(self.x, other, MOD))
        )

    __radd__ = __add__

    def __rsub__(self, other):
        return ModInt(other.x - self.x) if isinstance(other, ModInt) else ModInt(other - self.x)

    __rmul__ = __mul__

    def __rtruediv__(self, other):
        return (
            ModInt(other.x * pow(self.x, MOD - 2, MOD))
            if isinstance(other, ModInt)
            else ModInt(other * pow(self.x, MOD - 2, MOD))
        )

    def __rpow__(self, other):
        return (
            ModInt(pow(other.x, self.x, MOD))
            if isinstance(other, ModInt)
            else ModInt(pow(other, self.x, MOD))
        )


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    if N == K:
        ans = 1
        for a in A:
            ans *= a
            ans %= MOD
        print(ans)
        return

    B = []
    n_plus = 0
    for a in A:
        if a >= 0:
            B.append((a, True))
            n_plus += 1
        else:
            B.append((-a, False))

    if n_plus == 0:
        A.sort(reverse=True)
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= MOD
        print(ans)
        return

    B.sort(key=lambda x: x[0], reverse=True)
    cnt = 0
    prev_minus = None
    prev_plus = None
    ans = 1
    for i in range(K):
        b, flag = B[i]

        ans *= b
        ans %= MOD

        if flag:
            prev_plus = b
        else:
            cnt += 1
            prev_minus = b

    if cnt % 2 == 0 or ans == 0:
        print(ans)
        return

    next_minus = None
    next_plus = None
    for i in range(K, N):
        b, flag = B[i]
        if flag:
            if next_plus is None:
                next_plus = b
        else:
            if next_minus is None:
                next_minus = b

    ans = ModInt(ans)

    if next_plus is not None and prev_minus is not None:
        tmp1 = ans / ModInt(prev_minus)
        tmp1 *= ModInt(next_plus)
        tmp1 %= MOD
    else:
        tmp1 = -INF

    if next_minus is not None and prev_plus is not None:
        tmp2 = ans / ModInt(prev_plus)
        tmp2 *= ModInt(next_minus)
        tmp2 %= MOD
    else:
        tmp2 = -INF

    print(max(tmp1, tmp2))


if __name__ == "__main__":
    main()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    if N == K:
        ans = 1
        for a in A:
            ans *= a
            ans %= MOD
        print(ans)
        return

    B = []
    n_plus = 0
    for a in A:
        if a >= 0:
            B.append((a, True))
            n_plus += 1
        else:
            B.append((-a, False))

    if n_plus == 0:
        if K % 2 == 0:
            A.sort()
        else:
            A.sort(reverse=True)
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= MOD
        print(ans)
        return

    B.sort(key=lambda x: x[0], reverse=True)
    cnt = 0
    prev_minus = None
    prev_plus = None
    for i in range(K):
        b, flag = B[i]

        if flag:
            prev_plus = b
        else:
            cnt += 1
            prev_minus = b

    if prev_plus == 0:
        print(0)
        return

    if cnt % 2 == 0:
        ans = 1
        for i in range(K):
            b, flag = B[i]
            ans *= b
            ans %= MOD
        print(ans)
        return

    next_minus = None
    next_plus = None
    for i in range(K, N):
        b, flag = B[i]
        if flag:
            if next_plus is None:
                next_plus = b
        else:
            if next_minus is None:
                next_minus = b

        if b == 0:
            if next_minus is None:
                next_minus = 0

    if next_plus is not None and prev_minus is not None:
        ans1 = 1
        ignored = False
        for i in range(K):
            b, flag = B[i]

            if ignored or prev_minus != b:
                ans1 *= b
                ans1 %= MOD
                continue

            if prev_minus == b:
                ignored = True

        ans1 *= next_plus
        ans1 %= MOD
    else:
        ans1 = -INF

    if next_minus is not None and prev_plus is not None:
        ans2 = 1
        ignored = False
        for i in range(K):
            b, flag = B[i]

            if ignored or prev_plus != b:
                ans2 *= b
                ans2 %= MOD
                continue

            if prev_plus == b:
                ignored = True

        ans2 *= next_minus
        ans2 %= MOD
    else:
        ans2 = -INF

    if ans1 == -INF:
        print(ans2)
    elif ans2 == -INF:
        print(ans1)
    else:
        if prev_minus * next_minus > prev_plus * next_plus:
            print(ans2)
        else:
            print(ans1)


if __name__ == "__main__":
    main()

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

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


# mod m におけるaの逆元
def modinv(a, m):
    b = m
    u = 1
    v = 0
    while b:
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    u %= m

    # 負の場合は m を足して正の数にする
    if u < 0:
        u += m
    return u


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    B = []
    n_plus = 0
    for a in A:
        if a >= 0:
            B.append((a, True))
            n_plus += 1
        else:
            B.append((-a, False))

    if n_plus == 0:
        A.sort(reverse=True)
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= MOD
        print(ans)
        return

    B.sort(key=lambda x: x[0], reverse=True)
    cnt = 0
    prev_minus = None
    prev_plus = None
    ans = 1
    for i in range(K):
        b, flag = B[i]

        ans *= b
        ans %= MOD

        if flag:
            prev_plus = b
        else:
            cnt += 1
            prev_minus = b

    if cnt % 2 == 0 or ans == 0:
        print(ans)
        return

    if N == K:
        print(-ans)
        return

    next_minus = None
    next_plus = None
    for i in range(K, N):
        b, flag = B[i]
        if flag:
            if next_plus is None:
                next_plus = b
        else:
            if next_minus is None:
                next_minus = b

    if next_plus is not None and prev_minus is not None:
        inv = modinv(prev_minus, MOD)
        tmp1 = inv * ans
        tmp1 %= MOD
        tmp1 *= next_plus
        tmp1 %= MOD
    else:
        tmp1 = -INF

    if next_minus is not None and prev_plus is not None:
        inv = modinv(prev_plus, MOD)
        tmp2 = inv * ans
        tmp2 %= MOD
        tmp2 *= next_plus
        tmp2 %= MOD
    else:
        tmp2 = -INF

    print(max(tmp1, tmp2))


if __name__ == "__main__":
    main()


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    if N == K:
        ans = 1
        for a in A:
            ans *= a
            ans %= MOD
        print(ans)
        return

    B = []
    n_plus = 0
    for a in A:
        if a >= 0:
            B.append((a, True))
            n_plus += 1
        else:
            B.append((-a, False))

    if n_plus == 0:
        if K % 2 == 0:
            A.sort()
        else:
            A.sort(reverse=True)
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= MOD
        print(ans)
        return

    B.sort(key=lambda x: x[0], reverse=True)
    cnt = 0
    prev_minus = None
    prev_plus = None
    for i in range(K):
        b, flag = B[i]

        if flag:
            prev_plus = b
        else:
            cnt += 1
            prev_minus = b

    if prev_plus == 0:
        print(0)
        return

    if cnt % 2 == 0:
        ans = 1
        for i in range(K):
            b, flag = B[i]
            ans *= b
            ans %= MOD
        print(ans)
        return

    next_minus = None
    next_plus = None
    for i in range(K, N):
        b, flag = B[i]
        if flag:
            if next_plus is None:
                next_plus = b
        else:
            if next_minus is None:
                next_minus = b

        if b == 0:
            if next_minus is None:
                next_minus = 0

    if next_plus is not None and prev_minus is not None:
        ans1 = 1
        ignored = False
        for i in range(K):
            b, flag = B[i]

            if ignored or prev_minus != b:
                ans1 *= b
                ans1 %= MOD
                continue

            if prev_minus == b:
                ignored = True

        ans1 *= next_plus
        ans1 %= MOD
    else:
        ans1 = -INF

    if next_minus is not None and prev_plus is not None:
        ans2 = 1
        ignored = False
        for i in range(K):
            b, flag = B[i]

            if ignored or prev_plus != b:
                ans2 *= b
                ans2 %= MOD
                continue

            if prev_plus == b:
                ignored = True

        ans2 *= next_minus
        ans2 %= MOD
    else:
        ans2 = -INF

    if ans1 == -INF:
        print(ans2)
    elif ans2 == -INF:
        print(ans1)
    else:
        if prev_minus * next_minus > prev_plus * next_plus:
            print(ans2)
        else:
            print(ans1)


if __name__ == "__main__":
    main()