Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    ans = [-float('inf'),0]
    mod = 10**9+7
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    minus = []
    plus = []
    for i in range(N):
        if A[i]>=0:
            plus.append(A[i])
        else:
            minus.append(A[i])
    plus.sort()
    minus.sort(reverse=True)
    p = len(plus)
    m = len(minus)
    plus_large = [[0,1] for _ in range(p+1)]
    plus_small = [[0,1] for _ in range(p+1)]
    minus_large = [[0,1] for _ in range(m+1)]
    minus_small = [[0,1] for _ in range(m+1)]
    for i in range(p):
        plus_large[i+1][1] = plus_large[i][1]*plus[p-i-1]%mod
        plus_large[i+1][0] = plus_large[i][0]*plus[p-i-1]+plus_large[i][1]*plus[p-i-1]//mod
        plus_small[i+1][1] = plus_small[i][1]*plus[i]%mod
        plus_small[i+1][0] = plus_small[i][0]*plus[i]+plus_small[i][1]*plus[i]//mod
    for i in range(m):
        minus_large[i+1][1] = minus_large[i][1]*minus[m-i-1]%mod
        minus_large[i+1][0] = minus_large[i][0]*minus[m-i-1]+minus_large[i][1]*minus[m-i-1]//mod
        minus_small[i+1][1] = minus_small[i][1]*minus[i]%mod
        minus_small[i+1][0] = minus_small[i][0]*minus[i]+minus_small[i][1]*minus[i]//mod
    
    for mi in range(K+1): 
        pl = K-mi
        if p>=K-mi and m>=mi:
            if mi%2==0: #正の数
                a,b = plus_large[pl]
                c,d = minus_large[mi]
            else: #負の数
                a,b = plus_small[pl]
                c,d = minus_small[mi]
            f = b*d%mod
            e = a*c*mod + b*c + a*d + b*d//mod
            if e>ans[0] or (e==ans[0] and f>ans[1]):
                ans = [e,f]
    return ans[1]
print(solve())

Fixed submission:
from collections import defaultdict
def prod(A,k,mod):
  ans = 1
  for i in range(k):
      ans *= A[i]
      ans %= mod
  return ans
def solve():
  N, K = map(int, input().split())
  A = list(map(int, input().split()))
  mod = 10**9+7
  ans = 1
  if K==N:
    return prod(A,N,mod)
  minus = len([a for a in A if a<0])
  if minus==N:
    if K%2==0:
      A.sort()
    else:
      A.sort(reverse=True)
    return prod(A,K,mod)
  if K==1:
    return max(A)
  A.sort(key=lambda x:-abs(x))
  if A[K]==0:
    return 0
  minus = len([a for a in A[:K] if a<0])
  flag = [1]*K+[0]*(N-K)
  d = defaultdict(lambda: -1)
  if minus%2==1:
    for i in range(K,N):
      if A[i]>=0:
        d['p2'] = i
        break
    for i in range(K,N):
      if A[i]<0:
        d['m2'] = i
        break
    for i in range(K-1,-1,-1):
      if A[i]>0:
        d['p1'] = i
        break
    for i in range(K-1,-1,-1):
      if A[i]<0:
        d['m1'] = i
        break
    if d['p2']==-1 or d['m1']==-1:
      flag[d['m2']],flag[d['p1']] = 1,0
    elif d['m2']==-1 or d['p1']==-1:
      flag[d['p2']],flag[d['m1']] = 1,0
    elif A[d['p2']]*A[d['p1']]>A[d['m1']]*A[d['m2']]:
        flag[d['p2']],flag[d['m1']] = 1,0
    else:
      flag[d['m2']],flag[d['p1']] = 1,0
    ans = 1
    for i in range(N):
      if flag[i]==1:
        ans *= A[i]
        ans %= mod
    return ans
  return prod(A,K,mod)
print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    ans = [-float('inf'),0]
    mod = 10**9+7
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    minus = []
    plus = []
    for i in range(N):
        if A[i]>=0:
            plus.append(A[i])
        else:
            minus.append(A[i])
    plus.sort()
    minus.sort(reverse=True)
    p = len(plus)
    m = len(minus)
    plus_large = [[0,1] for _ in range(p+1)]
    plus_small = [[0,1] for _ in range(p+1)]
    minus_large = [[0,1] for _ in range(m+1)]
    minus_small = [[0,1] for _ in range(m+1)]
    for i in range(p):
        plus_large[i+1][1] = plus_large[i][1]*plus[p-i-1]%mod
        plus_large[i+1][0] = plus_large[i][0]*plus[p-i-1]+plus_large[i][1]*plus[p-i-1]//mod
        plus_small[i+1][1] = plus_small[i][1]*plus[i]%mod
        plus_small[i+1][0] = plus_small[i][0]*plus[i]+plus_small[i][1]*plus[i]//mod
    for i in range(m):
        minus_large[i+1][1] = minus_large[i][1]*minus[m-i-1]%mod
        minus_large[i+1][0] = minus_large[i][0]*minus[m-i-1]+minus_large[i][1]*minus[m-i-1]//mod
        minus_small[i+1][1] = minus_small[i][1]*minus[i]%mod
        minus_small[i+1][0] = minus_small[i][0]*minus[i]+minus_small[i][1]*minus[i]//mod
    
    for mi in range(K+1): 
        pl = K-mi
        if p>=K-mi and m>=mi:
            if mi%2==0: #正の数
                a,b = plus_large[pl]
                c,d = minus_large[mi]
            else: #負の数
                a,b = plus_small[pl]
                c,d = minus_small[mi]
            f = b*d%mod
            e = a*c*mod + b*c + a*d + b*d//mod
            if e>ans[0] or (e==ans[0] and f>ans[1]):
                ans = [e,f]
    return ans[1]
print(solve())

Fixed submission:
from collections import defaultdict
def prod(A,k,mod):
  ans = 1
  for i in range(k):
      ans *= A[i]
      ans %= mod
  return ans
def solve():
  N, K = map(int, input().split())
  A = list(map(int, input().split()))
  mod = 10**9+7
  ans = 1
  if K==N:
    return prod(A,N,mod)
  minus = len([a for a in A if a<0])
  if minus==N:
    if K%2==0:
      A.sort()
    else:
      A.sort(reverse=True)
    return prod(A,K,mod)
  if K==1:
    return max(A)
  A.sort(key=lambda x:-abs(x))
  if A[K]==0:
    return 0
  minus = len([a for a in A[:K] if a<0])
  flag = [1]*K+[0]*(N-K)
  d = defaultdict(lambda: -1)
  if minus%2==1:
    for i in range(K,N):
      if A[i]>=0:
        d['p2'] = i
        break
    for i in range(K,N):
      if A[i]<0:
        d['m2'] = i
        break
    for i in range(K-1,-1,-1):
      if A[i]>0:
        d['p1'] = i
        break
    for i in range(K-1,-1,-1):
      if A[i]<0:
        d['m1'] = i
        break
    if d['p2']==-1 or d['m1']==-1:
      flag[d['m2']],flag[d['p1']] = 1,0
    elif d['m2']==-1 or d['p1']==-1:
      flag[d['p2']],flag[d['m1']] = 1,0
    elif A[d['p2']]*A[d['p1']]>A[d['m1']]*A[d['m2']]:
        flag[d['p2']],flag[d['m1']] = 1,0
    else:
      flag[d['m2']],flag[d['p1']] = 1,0
    ans = 1
    for i in range(N):
      if flag[i]==1:
        ans *= A[i]
        ans %= mod
    return ans
  return prod(A,K,mod)
print(solve())