Problem p02616 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

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
    
    if all(a < 0 for a in A) and K%2 == 1:
        A.sort(reverse = True)
        ans = 1
        for i in range(K):
            ans *= A[i]
            ans %= MOD
        print(ans)
        return
    
    A.sort(key = abs,reverse = True)
    cnt_m = 0
    ans = 1
    last_p = -1
    last_m = 1
    for i in range(K):
        if A[i] == 0:
            print(0)
            return
        elif A[i] < 0:
            ans *= A[i]
            ans %= MOD
            cnt_m += 1
            last_m = A[i]
        else:
            ans *= A[i]
            ans %= MOD
            last_p = A[i]
    if cnt_m%2 == 0:
        print(ans)
    else:
        diff1 = last_p
        diff2 = last_m
        inv1 = pow(last_m,MOD - 2,MOD)
        inv2 = pow(last_p,MOD - 2,MOD)
        for i in range(K,N):
            if A[i] >= 0:
                diff1 *= A[i]
                inv1 *= A[i]
                inv1 %= MOD
                break
            if i == N - 1:
                diff1 = -1
        for i in range(K,N):
            if A[i] < 0:
                diff2 *= A[i]
                inv2 *= A[i]
                inv2 %= MOD
                break
            if i == N - 1:
                diff2 = -1
        
        if last_p > 0:
            if diff1 < diff2:
                ans *= inv2
                ans %= MOD
            else:
                ans *= inv1
                ans %= MOD
        else:
            ans *= inv1
            ans %= MOD
        print(ans)
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

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

    Z = sum(1 if a == 0 else 0 for a in A)
    if Z + K > N:
        print(0)
        return
    P = []
    M = []
    for a in A:
        if a > 0:
            P.append(a)
        else:
            M.append(a)
    
    if len(P) == 0:
        M.sort(reverse = True)
        ans = 1
        for i in range(K):
            ans *= M[i]
            ans %= MOD
        print(ans)
        return
    
    if Z + K == N:
        ans = 1
        for p in P:
            ans *= p
            ans %= MOD
        for m in M:
            ans *= m
            ans %= MOD
        print(ans)
        return

    if K%2 == 0:
        P.sort(reverse = True)
        number = []
        for i in range(0,len(P) - 1,2):
            number.append((P[i]*P[i + 1],0))

        M.sort()
        for i in range(0,len(M) - 1,2):
            number.append((M[i]*M[i + 1],1))
        
        number.sort(reverse = True)
        cnt = 0
        cnt_p = 0
        cnt_m = 0
        i = 0
        ans = 1
        while i < len(number):
            if cnt + 2 <= K:
                cnt += 2
                ans *= number[i][0]
                ans %= MOD
                if number[i][1] == 0:
                    cnt_p += 2
                else:
                    cnt_m += 2
            i += 1
        print(ans)
    
    else:
        P.sort(reverse = True)
        M.sort()

        ret = 1
        idx = 0
        midx = 0
        for i in range(0,len(P),2):
            if K - i - 1 <= len(M):
                idx = i
                for j in range(i + 1):
                    ret *= P[j]
                    ret %= MOD
                for j in range(K - i - 1):
                    ret *= M[j]
                    ret %= MOD
                midx = K - i - 2
                break
        
        diff = 1
        inv = 1
        for i in range(idx,len(P) - 2,2):
            if midx - 1 < 0:
                break
            diff *= P[i + 1]/M[midx]*P[i + 2]/M[midx - 1]
            inv *= P[i + 1]*P[i + 2]%MOD*pow(M[midx],MOD - 2,MOD)*pow(M[midx - 1],MOD - 2,MOD)%MOD
            if diff > 1:
                ret *= inv
                ret %= MOD
                inv = 1
                diff = 1
        print(ret)
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

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

    Z = sum(1 if a == 0 else 0 for a in A)
    if Z + K > N:
        print(0)
        return
    P = []
    M = []
    for a in A:
        if a > 0:
            P.append(a)
        else:
            M.append(a)
    
    if len(P) == 0:
        M.sort(reverse = True)
        ans = 1
        for i in range(K):
            ans *= M[i]
            ans %= MOD
        print(ans)
        return
    
    if Z + K == N:
        ans = 1
        for p in P:
            ans *= p
            ans %= MOD
        for m in M:
            ans *= m
            ans %= MOD
        print(ans)
        return

    if K%2 == 0:
        P.sort(reverse = True)
        number = []
        for i in range(0,len(P) - 1,2):
            number.append((P[i]*P[i + 1],0))

        M.sort()
        for i in range(0,len(M) - 1,2):
            number.append((M[i]*M[i + 1],1))
        
        number.sort(reverse = True)
        cnt = 0
        cnt_p = 0
        cnt_m = 0
        i = 0
        ans = 1
        while i < len(number):
            if cnt + 2 <= K:
                cnt += 2
                ans *= number[i][0]
                ans %= MOD
                if number[i][1] == 0:
                    cnt_p += 2
                else:
                    cnt_m += 2
            i += 1
        if cnt < K:
            ans *= P[cnt_p]
            ans *= M[cnt_m]
            ans %= MOD
        print(ans)
    
    else:
        P.sort(reverse = True)
        M.sort()

        ret = 1
        idx = 0
        midx = 0
        for i in range(0,len(P),2):
            if K - i - 1 <= len(M):
                idx = i
                for j in range(i + 1):
                    ret *= P[j]
                    ret %= MOD
                for j in range(K - i - 1):
                    ret *= M[j]
                    ret %= MOD
                midx = K - i - 2
                break
        
        diff = 1
        for i in range(idx,len(P) - 2,2):
            if midx - 1 < 0:
                break
            diff *= P[i + 1]/M[midx]*P[i + 2]/M[midx - 1]
            if diff > 1:
                ret *= diff
                ret //= 1
                diff = 1
        ret %= MOD
        print(ret)
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

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

    Z = sum(1 if a == 0 else 0 for a in A)
    if Z + K > N:
        print(0)
        return
    P = []
    M = []
    for a in A:
        if a > 0:
            P.append(a)
        else:
            M.append(a)
    
    if len(P) == 0:
        M.sort(reverse = True)
        ans = 1
        for i in range(K):
            ans *= M[i]
            ans %= MOD
        print(ans)
        return
    
    if K%2 == 0:
        P.sort(reverse = True)
        number = []
        for i in range(0,len(P) - 1,2):
            number.append((P[i]*P[i + 1],0))

        M.sort()
        for i in range(0,len(M) - 1,2):
            number.append((M[i]*M[i + 1],1))
        
        number.sort(reverse = True)
        cnt = 0
        cnt_p = 0
        cnt_m = 0
        i = 0
        ans = 1
        while i < len(number):
            if cnt + 2 <= K:
                cnt += 2
                ans *= number[i][0]
                ans %= MOD
                if number[i][1] == 0:
                    cnt_p += 2
                else:
                    cnt_m += 2
            i += 1
        if cnt < K:
            ans *= P[cnt_p]
            ans *= M[cnt_m]
            ans %= MOD
        print(ans)
    
    else:
        ans = 1
        P.sort(reverse = True)
        number = []
        for i in range(0,len(M) - 1,2):
            number.append((P[i]*P[i + 1],0))
        M.sort()
        idx = 0
        if len(P)%2 == 0:
            while K > len(P):
                ans *= M[idx]
                if idx + 1 < len(M):
                    ans *= M[idx + 1]
                    ans %= MOD
                else:
                    K -= 1
                    break
                idx += 2
                K -= 2
        for i in range(idx,len(M) - 1,2):
            number.append((M[i]*M[i + 1],1))
        
        number.sort(reverse = True)
        cnt = 0
        i = 0
        cnt_p = 0
        while i < len(number):
            if cnt + 2 <= K:
                cnt += 2
                ans *= number[i][0]
                if number[i][1] == 0:
                    cnt_p += 2
                ans %= MOD
            i += 1
        ans *= P[cnt_p]
        ans %= MOD
        print(ans)
if __name__ == '__main__':
    main()