Buggy python submission (verdict: Time Limit Exceeded):
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 10 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

MOD = 10 ** 9 + 7
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(sorted(A,key=lambda x: abs(x), reverse=True))

if K == N:
    ans = 1
    for a in A:
        ans *= a
        ans %= MOD
    print(ans % MOD)
    exit()
if K % 2 == 1 and len(list(filter(lambda x: x >= 0, A))) == 0:
    ans = 1
    for i in range(K):
        ans *= B[len(B) - 1 - i]
        ans %= MOD
    print(ans % MOD)
    exit()


ps = list(filter(lambda x: x >= 0, B))
ns = list(filter(lambda x: x < 0, B))

X = []
pi = 0
ni = 0
while len(X) < K:
    if pi + 1 < len(ps) and ni + 1 < len(ns):
        p = ps[pi] * ps[pi + 1]
        neg = ns[ni] * ns[ni + 1]
        if p > neg or K - 1 == len(X):
            X.append(ps[pi])
            pi += 1
        else:
            X.append(ns[ni])
            X.append(ns[ni + 1])
            ni += 2
    elif pi + 1 == len(ps):
        X.append(ps[pi])
        pi += 1
ans = 1
for x in X:
    ans *= x
    ans %= MOD
print(ans % MOD)


Fixed submission:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

S = []
T = []
for a in A:
    if a < 0:
        T.append(a)
    else:
        S.append(a)

ls = len(S)
lt = len(T)
ok = False

if ls > 0:
    if N == K:
        ok = lt % 2 == 0
    else:
        ok = True
else:
    ok = K % 2 == 0

ans = 1
if not ok:
    A = sorted(A, key=lambda x: abs(x))
    for a in A[:K]:
        ans *= a
        ans %= MOD
else:
    S = sorted(S)
    T = sorted(T, reverse=True)
    if K % 2 == 1: # if K is odd, the answer has at least one positive number.
        ans *= S.pop()
        ans %= MOD
    p = []
    # the num of negatives must be even, so positives must do the same
    # thus, we can pop 2 items at one time.
    while len(S) >= 2:
        x = S.pop()  
        y = S.pop()
        p.append(x * y)
    while len(T) >= 2:
        x = T.pop()  
        y = T.pop()
        p.append(x * y)
    p = sorted(p, reverse=True)
    for i in range(K // 2):
        ans *= p[i]
        ans %= MOD
print(ans % MOD)

Buggy python submission (verdict: Runtime Error):
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 10 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

MOD = 10 ** 9 + 7
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(sorted(A,key=lambda x: abs(x), reverse=True))

if K == N:
    ans = 1
    for a in A:
        ans *= a
        ans %= MOD
    print(ans % MOD)
    exit()
if K % 2 == 1 and len(list(filter(lambda x: x >= 0, A))) == 0:
    ans = 1
    for i in range(K):
        ans *= B[len(B) - 1 - i]
        ans %= MOD
    print(ans % MOD)
    exit()


ps = list(filter(lambda x: x >= 0, B))
ns = list(filter(lambda x: x < 0, B))

X = []
pi = 0
ni = 0
while len(X) < K:
    if pi + 1 < len(ps) and ni + 1 < len(ns):
        p = ps[pi] * ps[pi + 1]
        neg = ns[ni] * ns[ni + 1]
        if p > neg or K - 1 == len(X):
            X.append(ps[pi])
            pi += 1
        else:
            X.append(ns[ni])
            X.append(ns[ni + 1])
            ni += 2
    elif pi + 1 == len(ps):
        X.append(ps[pi + 1])
        pi += 1
ans = 1
for x in X:
    ans *= x
    ans %= MOD
print(ans % MOD)


Fixed submission:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

S = []
T = []
for a in A:
    if a < 0:
        T.append(a)
    else:
        S.append(a)

ls = len(S)
lt = len(T)
ok = False

if ls > 0:
    if N == K:
        ok = lt % 2 == 0
    else:
        ok = True
else:
    ok = K % 2 == 0

ans = 1
if not ok:
    A = sorted(A, key=lambda x: abs(x))
    for a in A[:K]:
        ans *= a
        ans %= MOD
else:
    S = sorted(S)
    T = sorted(T, reverse=True)
    if K % 2 == 1: # if K is odd, the answer has at least one positive number.
        ans *= S.pop()
        ans %= MOD
    p = []
    # the num of negatives must be even, so positives must do the same
    # thus, we can pop 2 items at one time.
    while len(S) >= 2:
        x = S.pop()  
        y = S.pop()
        p.append(x * y)
    while len(T) >= 2:
        x = T.pop()  
        y = T.pop()
        p.append(x * y)
    p = sorted(p, reverse=True)
    for i in range(K // 2):
        ans *= p[i]
        ans %= MOD
print(ans % MOD)