Problem p02616 (python)

Fixed submission:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, K = MAP()
A = LIST()

if N == K:
	ans = 1
	for x in A:
		ans = ans*x%mod
	print(ans)
	exit()

a = []
for x in A:
	flg = 0
	if x < 0:
		flg = 1
	a.append([x, abs(x), flg])

a.sort(key = lambda x: -x[1])

#print(a)

flg_sum = sum([x[2] for x in a[:K]])

if flg_sum%2 == 0:
	ans = 1
	for x in a[:K]:
		ans = ans*x[1]%mod
	print(ans)
	exit()


flg = a[K-1][2]
s0 = 0
s1 = 0
for i in range(K, N):
	if a[i][2] != flg:
		s0 = a[K-1][1]
		s1 = a[i][1]
		break

flg = a[K][2]
t0 = 0
t1 = 0
for i in range(K-1, -1, -1):
	if a[i][2] != flg:
		t0 = a[i][1]
		t1 = a[K][1]
		break

if s0+t0 != 0:
	ans = 1
	if s0 == 0:
		for i in range(K):
			if a[i][1] != t0:
				ans = ans*a[i][1]%mod
		print(ans*t1%mod)
		exit()
	elif t0 == 0:
		for i in range(K-1):
			ans = ans*a[i][1]%mod
		print(ans*s1%mod)
		exit()
	elif s1*t0 >= s0*t1 or t0 == 0:
		for i in range(K-1):
			ans = ans*a[i][1]%mod
		print(ans*s1%mod)
		exit()
	else:
		for i in range(K):
			if a[i][1] != t0:
				ans = ans*a[i][1]%mod
		print(ans*t1%mod)
		exit()


ans = 1
for x in a[::-1][:K]:
	ans = ans*x[0]%mod

print(ans)

Buggy submission (verdict: Runtime Error):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, K = MAP()
A = LIST()

a = []
for x in A:
	flg = 0
	if x < 0:
		flg = 1
	a.append([x, abs(x), flg])

a.sort(key = lambda x: -x[1])

#print(a)

flg_sum = sum([x[2] for x in a[:K]])

if flg_sum%2 == 0:
	ans = 1
	for x in a[:K]:
		ans = ans*x[1]%mod
	print(ans)
	exit()


flg = a[K-1][2]
for i in range(K, N):
	if a[i][2] != flg:
		ans = 1
		for j in range(K-1):
			ans = ans*a[j][1]%mod
		print(ans*a[i][1]%mod)
		exit()

for i in range(K-1, -1, -1):
	if a[i][2] != flg:
		ans = 1
		for j in range(K+1):
			if j != i:
				ans = ans*a[j][1]%mod
		print(ans)
		exit()

ans = 1
for x in a[::-1][:K]:
	ans = ans*x[0]%mod

print(ans)