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


def out(X):
    return list(itertools.accumulate(X, func=lambda x, y: x * y % mod))[-1]


N, K = [int(_) for _ in input().split()]
A = sorted([int(_) for _ in input().split()], key=lambda x: -abs(x))
mod = 10**9 + 7
cnt = 0
ipos = ineg = -1
for i, a in enumerate(A[:K]):
    if a < 0:
        cnt += 1
        ineg = i
    else:
        ipos = i
if cnt % 2 == 0:
    print(out(A[:K]))
else:
    oneg = opos = -10**10
    for i, a in enumerate(A[K:]):
        if a < 0 and ipos != -1:
            X = A[:ipos] + A[ipos + 1:K] + A[K + i]
            oneg = out(X)
            ipos = -1
        elif a > 0 and ineg != -1:
            X = A[:ineg] + A[ineg + 1:K] + A[K + i]
            opos = out(X)
            ineg = -1
    if oneg != -10**10 or opos != -10**10:
        print(max(opos, oneg))
    else:
        C = A[::-1]
        print(out(C[:K]))


Fixed submission:
import itertools


def out(X):
    return list(itertools.accumulate(X, func=lambda x, y: x * y % mod))[-1]


N, K = [int(_) for _ in input().split()]
A = sorted([int(_) for _ in input().split()], key=lambda x: -abs(x))
mod = 10**9 + 7
cnt = 0
ipos = ineg = -1
for i, a in enumerate(A[:K]):
    if a < 0:
        cnt += 1
        ineg = i
    else:
        ipos = i
if cnt % 2 == 0:
    print(out(A[:K]))
else:
    oneg = opos = -10**10
    for i, a in enumerate(A[K:]):
        if a < 0 and ipos != -1:
            X = A[:ipos] + A[ipos + 1:K] + [A[K + i]]
            oneg = out(X)
            beforeneg = A[ipos]
            afterneg = A[K + i]
            ipos = -1
        elif a > 0 and ineg != -1:
            Y = A[:ineg] + A[ineg + 1:K] + [A[K + i]]
            opos = out(Y)
            beforepos = A[ineg]
            afterpos = A[K + i]
            ineg = -1
    if oneg != -10**10 and opos != -10**10:
        #afterneg/beforeneg > afterpos>beforepos
        if afterneg * beforepos > afterpos * beforeneg:
            print(oneg)
        else:
            print(opos)
    elif oneg != -10**10 or opos != -10**10:
        print(max(opos, oneg))
    else:
        C = A[::-1]
        print(out(C[:K]))

Buggy python submission (verdict: Runtime Error):
import itertools, operator


def out(X):
    print(list(itertools.accumulate(X, func=lambda x, y: x * y % mod))[-1])
    exit()


N, K = [int(_) for _ in input().split()]
A = sorted([int(_) for _ in input().split()], key=lambda x: -abs(x))
mod = 10**9 + 7
cnt = 0
ipos = ineg = -1
for i, a in enumerate(A[:K]):
    if a < 0:
        cnt += 1
        ineg = i
    else:
        ipos = i
if cnt % 2 == 0:
    out(A[:K])
else:
    for i, a in enumerate(A[K:]):
        if a < 0 and ipos != -1:
            X = A[:ipos] + A[ipos + 1:K] + A[K + i]
            out(X)
        elif a > 0 and ineg != -1:
            X = A[:ineg] + A[ineg + 1:K] + A[K + i]
            out(X)
    else:
        C = A[::-1]
        cnt = 0
        out(C[:K])


Fixed submission:
import itertools


def out(X):
    return list(itertools.accumulate(X, func=lambda x, y: x * y % mod))[-1]


N, K = [int(_) for _ in input().split()]
A = sorted([int(_) for _ in input().split()], key=lambda x: -abs(x))
mod = 10**9 + 7
cnt = 0
ipos = ineg = -1
for i, a in enumerate(A[:K]):
    if a < 0:
        cnt += 1
        ineg = i
    else:
        ipos = i
if cnt % 2 == 0:
    print(out(A[:K]))
else:
    oneg = opos = -10**10
    for i, a in enumerate(A[K:]):
        if a < 0 and ipos != -1:
            X = A[:ipos] + A[ipos + 1:K] + [A[K + i]]
            oneg = out(X)
            beforeneg = A[ipos]
            afterneg = A[K + i]
            ipos = -1
        elif a > 0 and ineg != -1:
            Y = A[:ineg] + A[ineg + 1:K] + [A[K + i]]
            opos = out(Y)
            beforepos = A[ineg]
            afterpos = A[K + i]
            ineg = -1
    if oneg != -10**10 and opos != -10**10:
        #afterneg/beforeneg > afterpos>beforepos
        if afterneg * beforepos > afterpos * beforeneg:
            print(oneg)
        else:
            print(opos)
    elif oneg != -10**10 or opos != -10**10:
        print(max(opos, oneg))
    else:
        C = A[::-1]
        print(out(C[:K]))