Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

mod = 1000000007

n,k = map(int,input().split())

a = list(map(int,input().split()))

plus_1 = deque()
minus_1 = deque()
plus_2 = deque()
minus_2 = deque()

a.sort()

for i in a:
    if i > 0:
        plus_2.append(i)
    else:
        minus_1.append(i)

a.sort(reverse=True)

for i in a:
    if i > 0:
        plus_1.append(i)
    else:
        minus_2.append(i)

maxa = 1

mini = 1

i = 0

while i < k:
    if len(plus_1) > 1 and len(minus_1) > 1:
        if plus_1[0]*plus_1[1] < minus_1[0]*minus_1[1] and i+2<=k:
            maxa *= minus_1[0]*minus_1[1]
            minus_1.popleft()
            minus_1.popleft()
            i += 2
        else:
            maxa *= plus_1[0]
            plus_1.popleft()
            i += 1
    elif len(plus_1):
        maxa *= plus_1[0]
        plus_1.popleft()
        i += 1
    else:
        maxa *= minus_1[0]
        minus_1.popleft()
        i += 1
if maxa > 0:
    print(maxa%mod)
    exit()

i = 0

while i < k:
    if len(plus_2) and len(minus_2):
        if plus_2[0] < abs(minus_2[0]):
            mini *= plus_2[0]
            plus_2.popleft()
        else:
            mini *= minus_2[0]
            minus_2.popleft()
    elif len(plus_2):
        mini *= plus_2[0]
        plus_2.popleft()
    else:
        mini *= minus_2[0]
        minus_2.popleft()
    i += 1

print(mini%mod)


Fixed submission:
MOD = 10 ** 9 + 7
 
N, K = map(int, input().split())
l = list(map(int, input().split()))
pos = []
neg = []
z = 0
 
for v in l:
    if v > 0:
        pos.append(v)
    elif v < 0:
        neg.append(v)
    else:
        z += 1
pos.sort()
neg.sort()
 
if N - z < K:
    print(0)
elif N - z == K:
    if len(neg) % 2 and z:
        print(0)
    else:
        out = 1
        for v in neg + pos:
            out *= v
            out %= 10 ** 9 + 7
        print(out)
elif len(pos) == 0:
    if K % 2:
        if z:
            print(0)
        else:
            neg.reverse()
            out = 1
            for i in range(K):
                out *= neg[i]
                out %= 10 ** 9 + 7
            print(out)
    else:
        out = 1
        for i in range(K):
            out *= neg[i]
            out %= 10 ** 9 + 7
        print(out)
else:
    out = 1
    neg.reverse()
    if K % 2:
        out = pos.pop()
        K -= 1
    while K:
        if len(pos) >= 2:
            nP = pos[-1] * pos[-2]
        else:
            nP = 0
 
        if len(neg) >= 2:
            nN = neg[-1] * neg[-2]
        else:
            nN = 0
 
        if nP > nN:
            pos.pop()
            pos.pop()
            out *= nP
        else:
            neg.pop()
            neg.pop()
            out *= nN
        out %= MOD
        K -= 2
    print(out)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

mod = 1000000007

n,k = map(int,input().split())

a = list(map(int,input().split()))

plus_1 = deque()
minus_1 = deque()
plus_2 = deque()
minus_2 = deque()

a.sort()

for i in a:
    if i > 0:
        plus_2.append(i)
    else:
        minus_1.append(i)

a.sort(reverse=True)

for i in a:
    if i > 0:
        plus_1.append(i)
    else:
        minus_2.append(i)

maxa = 1

mini = 1

i = 0

while i < k:
    if len(plus_1) > 1 and len(minus_1) > 1:
        if plus_1[0]*plus_1[1] < minus_1[0]*minus_1[1] and i+2<=k:
            maxa *= minus_1[0]*minus_1[1]
            minus_1.popleft()
            minus_1.popleft()
            i += 2
        else:
            maxa *= plus_1[0]
            plus_1.popleft()
            i += 1
    elif len(plus_1):
        maxa *= plus_1[0]
        plus_1.popleft()
        i += 1
    else:
        maxa *= minus_1[0]
        minus_1.popleft()
        i += 1

i = 0

while i < k:
    if len(plus_2) and len(minus_2):
        if plus_2[0] < abs(minus_2[0]):
            mini *= plus_2[0]
            plus_2.popleft()
        else:
            mini *= minus_2[0]
            minus_2.popleft()
    elif len(plus_2):
        mini *= plus_2[0]
        plus_2.popleft()
    else:
        mini *= minus_2[0]
        minus_2.popleft()
    i += 1

if maxa > 0:
    print(maxa%mod)
else:
    print(mini%mod)

Fixed submission:
MOD = 10 ** 9 + 7
 
N, K = map(int, input().split())
l = list(map(int, input().split()))
pos = []
neg = []
z = 0
 
for v in l:
    if v > 0:
        pos.append(v)
    elif v < 0:
        neg.append(v)
    else:
        z += 1
pos.sort()
neg.sort()
 
if N - z < K:
    print(0)
elif N - z == K:
    if len(neg) % 2 and z:
        print(0)
    else:
        out = 1
        for v in neg + pos:
            out *= v
            out %= 10 ** 9 + 7
        print(out)
elif len(pos) == 0:
    if K % 2:
        if z:
            print(0)
        else:
            neg.reverse()
            out = 1
            for i in range(K):
                out *= neg[i]
                out %= 10 ** 9 + 7
            print(out)
    else:
        out = 1
        for i in range(K):
            out *= neg[i]
            out %= 10 ** 9 + 7
        print(out)
else:
    out = 1
    neg.reverse()
    if K % 2:
        out = pos.pop()
        K -= 1
    while K:
        if len(pos) >= 2:
            nP = pos[-1] * pos[-2]
        else:
            nP = 0
 
        if len(neg) >= 2:
            nN = neg[-1] * neg[-2]
        else:
            nN = 0
 
        if nP > nN:
            pos.pop()
            pos.pop()
            out *= nP
        else:
            neg.pop()
            neg.pop()
            out *= nN
        out %= MOD
        K -= 2
    print(out)

Buggy python submission (verdict: Time Limit Exceeded):
import copy

mod = 1000000007

n,k = map(int,input().split())

a = list(map(int,input().split()))

plus_1 = []

minus_1 = []

for i in a:
    if i > 0:
        plus_1.append(i)
    else:
        minus_1.append(i)

plus_2 = plus_1.copy()
minus_2 = minus_1.copy()

plus_1.sort(reverse=True)
minus_1.sort()
maxa = 1

plus_2.sort()
minus_2.sort(reverse=True)
mini = 1

i = 0

while i < k:
    if len(plus_1) > 1 and len(minus_1) > 1:
        if plus_1[0]*plus_1[1] < minus_1[0]*minus_1[1] and i+2<=k:
            maxa *= minus_1[0]*minus_1[1]
            minus_1.pop(0)
            minus_1.pop(0)
            i += 2
        else:
            maxa *= plus_1[0]
            plus_1.pop(0)
            i += 1
    elif len(plus_1):
        maxa *= plus_1[0]
        plus_1.pop(0)
        i += 1
    else:
        maxa *= minus_1[0]
        minus_1.pop(0)
        i += 1

i = 0

while i < k:
    if len(plus_2) and len(minus_2):
        if plus_2[0] < abs(minus_2[0]):
            mini *= plus_2[0]
            plus_2.pop(0)
        else:
            mini *= minus_2[0]
            minus_2.pop(0)
    elif len(plus_2):
        mini *= plus_2[0]
        plus_2.pop(0)
    else:
        mini *= minus_2[0]
        minus_2.pop(0)
    i += 1

if maxa > 0:
    print(maxa%mod)
else:
    print(mini%mod)

Fixed submission:
MOD = 10 ** 9 + 7
 
N, K = map(int, input().split())
l = list(map(int, input().split()))
pos = []
neg = []
z = 0
 
for v in l:
    if v > 0:
        pos.append(v)
    elif v < 0:
        neg.append(v)
    else:
        z += 1
pos.sort()
neg.sort()
 
if N - z < K:
    print(0)
elif N - z == K:
    if len(neg) % 2 and z:
        print(0)
    else:
        out = 1
        for v in neg + pos:
            out *= v
            out %= 10 ** 9 + 7
        print(out)
elif len(pos) == 0:
    if K % 2:
        if z:
            print(0)
        else:
            neg.reverse()
            out = 1
            for i in range(K):
                out *= neg[i]
                out %= 10 ** 9 + 7
            print(out)
    else:
        out = 1
        for i in range(K):
            out *= neg[i]
            out %= 10 ** 9 + 7
        print(out)
else:
    out = 1
    neg.reverse()
    if K % 2:
        out = pos.pop()
        K -= 1
    while K:
        if len(pos) >= 2:
            nP = pos[-1] * pos[-2]
        else:
            nP = 0
 
        if len(neg) >= 2:
            nN = neg[-1] * neg[-2]
        else:
            nN = 0
 
        if nP > nN:
            pos.pop()
            pos.pop()
            out *= nP
        else:
            neg.pop()
            neg.pop()
            out *= nN
        out %= MOD
        K -= 2
    print(out)