Problem p02616 (python)

Fixed submission:
#時間の無駄なのでクソ問と割り切って飛ばす
def prod(array):
    res = 1
    for a, _ in array:
        res *= a
        res %= MOD
    return res

MOD = 10**9 + 7
N, K = map(int, input().split())
if N==K:
    res = 1
    for a in map(int, input().split()):
        res *= a
        res %= MOD
    print(res)
    exit()
A = []
allsgn = 0
for a in map(int, input().split()):
    A.append((abs(a), a < 0))
    allsgn += a<0
A.sort(reverse=True)
sgn = 0
s0 = -1
lastA = -1
for i, p in enumerate(A[:K]):
    a, s = p
    sgn += s
    if s != s0:
        lastA = i-1
    s0 = s
lasts = s0
if allsgn==N:
    if K%2==1:
        print((-prod(A[-K:]))%MOD)
        exit()
    else:
        print(prod(A[:K])%MOD)
        exit()
if sgn%2==0:
    print(prod(A[:K])%MOD)
    exit()
if lasts != A[K][1]:
    res = prod(A[:K-1])*A[K][0]
else:
    firstA = None
    for i, p in enumerate(A[K:],start=K):
        a, s = p
        if s != lasts:
            firstA = i
            break
    if lastA == -1 or (firstA is not None and A[K][0]/A[lastA][0] < A[firstA][0]/A[K-1][0]):
        res = prod(A[:K - 1]) * A[firstA][0]
    else:
        res = prod(A[:lastA]) * prod(A[lastA + 1:K + 1])
print(res%MOD)
"""
6 3
5 -3 -3 -3 -3 -2

10 7
-10 -10 -10 -10 10 10 10 10 10 10
"""

Buggy submission (verdict: Runtime Error):
def main():
    N, K = map(int, input().split())
    mod = 10**9 + 7
    A = list(map(int, input().split()))
    B = [(abs(x), x) for x in A]
    B.sort(reverse=True)
    plus = []
    minus = []

    ans = 1

    if N == K:
        for x in A:
            ans *= x
            ans %= mod
        print(ans)
        return
    minus_count = 0
    for i in range(K):
        if B[i][-1] < 0:
            minus_count += 1
            minus.append(B[i][-1])
        else:
            plus.append(B[i][-1])
        ans *= B[i][-1]
        ans %= mod

    if minus_count % 2 == 0:
        print(ans)
        return

    else:
        if len(plus) > 0 and len(minus) > 0:
            S = 1
            for i in range(len(plus)-1):
                S *= plus[i]
                S %= mod
            for i in range(len(minus)-1):
                S *= minus[i]
                S %= mod
            plus_sub = -1
            minus_sub = -1
            for i in range(K, N):
                if B[i][-1] >= 0:
                    plus_sub = B[i][-1]*plus[-1]
                    break
            for i in range(K, N):
                if B[i][-1] < 0:
                    minus_sub = B[i][-1]*minus[-1]
            print(S*max(plus_sub, minus_sub) % mod)
            return

        else:
            for i in range(K, N):
                if B[i][-1] >= 0:
                    minus.append(B[i][-1])
                    break
            if minus[-1] >= 0:
                res = 1
                for i in range(K-1):
                    res *= minus[i]
                    res %= mod
                res *= mimus[-1]
                print(res % mod)
                return
            else:
                A.sort(reverse=True)
                res = 1
                for i in range(K):
                    res *= A[i]
                    res %= mod
                print(res)
                return


main()

Buggy submission (verdict: Runtime Error):
def main():
    N, K = map(int, input().split())
    mod = 10**9 + 7
    A = list(map(int, input().split()))
    B = [(abs(x), x) for x in A]
    B.sort(reverse=True)
    plus = []
    minus = []

    ans = 1

    if N == K:
        for x in A:
            ans *= x
            ans %= mod
        print(ans)
        return
    minus_count = 0
    for i in range(K):
        if B[i][-1] < 0:
            minus_count += 1
            minus.append(B[i][-1])
        else:
            plus.append(B[i][-1])
        ans *= B[i][-1]
        ans %= mod

    if minus_count % 2 == 0:
        print(ans)
        return

    else:
        if len(plus) > 0 and len(minus) > 0:
            S = 1
            for i in range(len(plus)-1):
                S *= plus[i]
                S %= mod
            for i in range(len(minus)-1):
                S *= minus[i]
                S %= mod
            plus_sub = -1
            mimus_sub = -1
            for i in range(K, N):
                if B[i][-1] >= 0:
                    plus_sub = B[i][-1]*plus[-1]
                    break
            for i in range(K, N):
                if B[i][-1] < 0:
                    minus_sub = B[i][-1]*minus[-1]
            print(S*max(plus_sub, minus_sub) % mod)
            return

        else:
            for i in range(K, N):
                if B[i][-1] >= 0:
                    minus.append(B[i][-1])
                    break
            if minus[-1] >= 0:
                res = 1
                for i in range(K-1):
                    res *= minus[i]
                    res %= mod
                res *= mimus[-1]
                print(res % mod)
                return
            else:
                A.sort(reverse=True)
                res = 1
                for i in range(K):
                    res *= A[i]
                    res %= mod
                print(res)
                return


main()