Buggy python submission (verdict: Time Limit Exceeded):
def f(P,M):
    S=0
    K=1
    for p in P[::-1]:
        S+=K*p
        K*=M
    return S

def g(X,Y,M):
    a,b=len(X),len(Y)
    Q=[0]*(len(X)+len(Y)-1)

    X=X[::-1]
    Y=Y[::-1]
    for i in range(a):
        for j in range(b):
            Q[i+j]+=X[i]*Y[j]
    Q=Q[::-1]

    T=0
    for i in range(len(Q)-1,-1,-1):
        F=T+Q[i]
        T=F//M
        Q[i]=F%M

    if T==0:
        return Q
    else:
        return h(T,M)+Q
    
def h(N,M):
    W=[]
    while N>0:
        W=[N%M]+W
        N//=M
    return W

N,K=map(int,input().split())
A=list(map(int,input().split()))
M=10**9+7
        
P,Q=[],[] #P,Q...+,-の要素
Z=0 #Z...0の個数

for a in A:
    if a>0:
        P.append(h(a,M))
    elif a<0:
        Q.append(h(-a,M))
    else:
        Z+=1

P.sort(reverse=True)
X=[[1]]
for p in P:
    X.append(g(X[-1],p,M))

Q.sort()
Y=[[1]]
for q in Q:
    Y.append(g(Y[-1],q,M))

J=-1
S=len(P)
for m in range(0,len(Q)+1,2):
    if 0<=K-m<=S:
        J=max(J,f(g(X[K-m],Y[m],M),M))

if J>0:
    print(J%M)
else:
    if Z==0:
        J=[1]
        for i in range(K):
            J=g(J,Q[i],M)
        print((-f(J,M))%M)
    else:
        print(0)


Fixed submission:
from collections import deque
import sys

N,K=map(int,input().split())
A=list(map(int,input().split()))
M=10**9+7

if N==K:
    T=1
    for a in A:
        T=(T*a)%M
    print(T)
    sys.exit()
elif max(A)<0 and K%2==1:
    A.sort(reverse=True)
    T=1
    for i in range(K):
         T=(T*A[i])%M
    print(T)
    sys.exit()
    
U=list(map(lambda x:max(x,0),A))
U.sort(reverse=True)
U=deque(U)

V=list(map(lambda x:min(x,0),A))
V.sort()
V=deque(V)

T=1
Y=0
while Y<K:
    x1,x2=U[0],U[1]
    y1,y2=V[0],V[1]

    if Y==K-1 or x1*x2>=y1*y2:
        Y+=1
        T=(T*x1)%M
        _=U.popleft()
        U.append(0)
    else:
        Y+=2
        T=(T*y1*y2)%M
        _=V.popleft()
        _=V.popleft()
        V.append(0)
        V.append(0)
print(T)

Buggy python submission (verdict: Runtime Error):
def f(P,M):
    return P[0]*M+P[1]

def g(X,Y,M):
    (a,b)=X
    (x,y)=Y

    return (a*x*M+a*y+b*x+(y*b)//M,(y*b)%M)

N,K=map(int,input().split())
A=list(map(int,input().split()))
M=10**9+7

P,Q=[],[] #P,Q...+,-の要素
Z=0 #Z...0の個数

for a in A:
    if a>0:
        P.append(divmod(a,M))
    elif a<0:
        Q.append(divmod(a,M))
    else:
        Z+=1

P.sort(reverse=True)
X=[(0,1)]
(a,b)=(0,1)
for (x,y) in P:
    a,b=g((a,b),(x,y),M)
    X.append((a,b))

Q.sort()
Y=[(0,1)]
(a,b)=(0,1)
for (x,y) in Q:
    a,b=g((a,b),(x,y),M)
    Y.append((a,b))

J=-1
S=len(P)
for m in range(0,len(Q)+1,2):
    if 0<=K-m<=S:
        J=max(J,f(g(X[K-m],Y[m],M),M))

if J>0:
    print(J%M)
else:
    if Z==0:
        J=1
        for i in range(K):
            J*=Q[-(i+1)]
        print(J%M)
    else:
        print(0)


Fixed submission:
from collections import deque
import sys

N,K=map(int,input().split())
A=list(map(int,input().split()))
M=10**9+7

if N==K:
    T=1
    for a in A:
        T=(T*a)%M
    print(T)
    sys.exit()
elif max(A)<0 and K%2==1:
    A.sort(reverse=True)
    T=1
    for i in range(K):
         T=(T*A[i])%M
    print(T)
    sys.exit()
    
U=list(map(lambda x:max(x,0),A))
U.sort(reverse=True)
U=deque(U)

V=list(map(lambda x:min(x,0),A))
V.sort()
V=deque(V)

T=1
Y=0
while Y<K:
    x1,x2=U[0],U[1]
    y1,y2=V[0],V[1]

    if Y==K-1 or x1*x2>=y1*y2:
        Y+=1
        T=(T*x1)%M
        _=U.popleft()
        U.append(0)
    else:
        Y+=2
        T=(T*y1*y2)%M
        _=V.popleft()
        _=V.popleft()
        V.append(0)
        V.append(0)
print(T)

Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().split())
A=list(map(int,input().split()))
M=10**9+7

P,Q=[],[] #P,Q...+,-の要素
Z=0 #Z...0の個数

for a in A:
    if a>0:
        P.append(a)
    elif a<0:
        Q.append(a)
    else:
        Z+=1

P.sort(reverse=True)
X=[1]
for p in P:
    X.append(X[-1]*p)
    
Q.sort()
Y=[1]
for q in Q:
    Y.append(Y[-1]*q)

J=-1
S=len(P)
for m in range(0,len(Q)+1,2):
    if 0<=K-m<=S:
        J=max(J,X[K-m]*Y[m])

if J>0:
    print(J%M)
else:
    if Z==0:
        J=1
        for i in range(K):
            J*=Q[-(i+1)]
        print(J%M)
    else:
        print(0)


Fixed submission:
from collections import deque
import sys

N,K=map(int,input().split())
A=list(map(int,input().split()))
M=10**9+7

if N==K:
    T=1
    for a in A:
        T=(T*a)%M
    print(T)
    sys.exit()
elif max(A)<0 and K%2==1:
    A.sort(reverse=True)
    T=1
    for i in range(K):
         T=(T*A[i])%M
    print(T)
    sys.exit()
    
U=list(map(lambda x:max(x,0),A))
U.sort(reverse=True)
U=deque(U)

V=list(map(lambda x:min(x,0),A))
V.sort()
V=deque(V)

T=1
Y=0
while Y<K:
    x1,x2=U[0],U[1]
    y1,y2=V[0],V[1]

    if Y==K-1 or x1*x2>=y1*y2:
        Y+=1
        T=(T*x1)%M
        _=U.popleft()
        U.append(0)
    else:
        Y+=2
        T=(T*y1*y2)%M
        _=V.popleft()
        _=V.popleft()
        V.append(0)
        V.append(0)
print(T)