Problem:
 Score : 500 points    Problem Statement Given are N integers A_1,\ldots,A_N .  We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.  Then, print the maximum product modulo (10^9+7) , using an integer between 0 and 10^9+6 (inclusive).      Constraints  1 \leq K \leq N \leq 2\times 10^5  |A_i| \leq 10^9         Input Input is given from Standard Input in the following format:  N  K  A_1  \ldots  A_N       Output Print the maximum product modulo (10^9+7) , using an integer between 0 and 10^9+6 (inclusive).        Sample Input 1 4 2
1 2 -3 -4      Sample Output 1 12  The possible products of the two chosen elements are 2 , -3 , -4 , -6 , -8 , and 12 , so the maximum product is 12 .       Sample Input 2 4 3
-1 -2 -3 -4      Sample Output 2 1000000001  The possible products of the three chosen elements are -24 , -12 , -8 , and -6 , so the maximum product is -6 .  We print this value modulo (10^9+7) , that is, 1000000001 .       Sample Input 3 2 1
-1 1000000000      Sample Output 3 1000000000  The possible products of the one chosen element are -1 and 1000000000 , so the maximum product is 1000000000 .       Sample Input 4 10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1      Sample Output 4 999983200  Be sure to print the product modulo (10^9+7) .

Solution (Python):
def examA():
    N = I()
    ans = (10000 - N)%1000
    print(ans)
    return

def examB():
    T = ["AC","WA","TLE","RE"]
    N = I()
    S = [SI()for _ in range(N)]

    ans = [0]*4
    for s in S:
        for i in range(4):
            if s==T[i]:
                ans[i] += 1
                break
    for i in range(4):
        print(T[i]+" x "+str(ans[i]))
    return

def examC():
    H, W, K = LI()
    C = [[""]*W for _ in range(H)]
    for h in range(H):
        S = SI()
        for w in range(W):
            C[h][w] = S[w]
    ans = 0
    
    for hi in range(1<<H):
        nowC = deepcopy(C)
        for h in range(H):
            if hi&(1<<h):
                for i in range(W):
                    nowC[h][i] = "R"
        for wi in range(1 << W):
            nowCC = deepcopy(nowC)
            for w in range(W):
                if wi & (1 << w):
                    for i in range(H):
                        nowCC[i][w] = "R"
            cnt = 0
            for h in range(H):
                for w in range(W):
                    if nowCC[h][w] == "
                        cnt += 1
            if cnt == K:
                
                ans += 1
    print(ans)
    return

def examD():
    N = I()
    A = LI()
    A.sort(reverse=True)
    ans = 0
    ans += A[0]
    cur = 0
    for i in range(2,N):
        if i%2==0:
            cur += 1
        ans += A[cur]
    print(ans)
    return

def examE():
    N, K = LI()
    A = LI()
    A_neg = []; neg = 0
    A_0 = []; num_0 = 0
    A_pos = []; pos = 0
    for a in A:
        if a<0:
            A_neg.append(-a)
            neg += 1
        elif a==0:
            A_0.append(a)
            num_0 += 1
        else:
            A_pos.append(a)
            pos += 1
    if N-num_0<K:
        print(0)
        return
    A_neg.sort()
    A_pos.sort()
    if pos==0:
        ans = 1
        if K%2==0:
            ans = 1
            for i in range(neg-K,neg):
                ans *= A_neg[i]
                ans %= mod
        else:
            for i in range(K):
                ans *= A_neg[i]
                ans %= mod
            ans = mod - ans
            if num_0>0:
                ans = 0
        print(ans)
        return

    if neg==0:
        ans = 1
        for i in range(pos - K, pos):
            ans *= A_pos[i]
            ans %= mod
        print(ans)
        return

    A_neg.sort(reverse=True)
    A_pos.sort(reverse=True)

    now_p = 0
    now_n = 0
    ans = 1

    if pos+neg==K:
        for i in range(pos):
            ans *= A_pos[i]
            ans %= mod
        for i in range(neg):
            ans *= A_neg[i]
            ans %= mod
        if neg%2==1:
            ans = mod-ans
        print(ans)
        return

    if K%2==1:
        ans *= A_pos[now_p]
        now_p += 1

    while (now_n + now_p < K):
        if now_n + 1 >= neg:
            ans *= A_pos[now_p] * A_pos[now_p + 1]
            ans %= mod
            now_p += 2
            continue
        if now_p + 1 >= pos:
            ans *= A_neg[now_n] * A_neg[now_n + 1]
            ans %= mod
            now_n += 2
            continue

        if A_pos[now_p] * A_pos[now_p + 1] >= A_neg[now_n] * A_neg[now_n + 1]:
            ans *= A_pos[now_p] * A_pos[now_p + 1]
            ans %= mod
            now_p += 2
        else:
            ans *= A_neg[now_n] * A_neg[now_n + 1]
            ans %= mod
            now_n += 2

    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

from decimal import getcontext,Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = dec("0.000000000001")
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}

getcontext().prec = 28

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examE()

"""
6
8 7 6 5 4 3


5 5
-1 -2 3 4 5
"""

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(int, input().split()))
if N == 1:
  print(max(a))
  exit(0)

mod = 10 ** 9 + 7
mn = []
zr = 0
pl = []
t = []
for x in a:
  if x < 0:
    mn.append(-x)
    t.append((-x, 0))
  elif x == 0: zr += 1
  else:
    pl.append(x)
    t.append((x, 1))

if N - zr < K:
  print(0)
  exit(0)

t.sort(reverse = True)

c = 0
res = 1
lpl = -1
lmn = -1
for x, cc in t[: K]:
  res *= x
  res %= mod
  c ^= (cc == 0)
  if cc: lpl = x
  else: lmn = x
if c == 0:
  print(res)
  exit(0)

respl = -1
resmn = -1
plt = 0
mnt = 0
if lpl != -1:
  respl = res + 0
  for x, cc in t[K: ]:
    if cc == 0:
      respl *= pow(lpl, mod - 2, mod)
      respl %= mod
      respl *= x
      respl %= mod
      plt = x
      break
  else: respl = -1

if lmn != -1:
  resmn = res + 0
  for x, cc in t[K: ]:
    if cc:
      resmn *= pow(lmn, mod - 2, mod)
      resmn %= mod
      resmn *= x
      resmn %= mod
      mnt = x
      break
  else: resmn = -1

if (respl != -1) ^ (resmn != -1):
  print(max(respl, resmn))
  exit(0)
if (respl != -1) and (resmn != -1):
  if plt * lmn > mnt * lpl: print(respl)
  else: print(resmn)
  exit(0)

if len(pl) >= K:
  res = 1
  for x in pl[: K]:
    res *= x
    res %= mod
  print(res)
  exit(0)
if zr:
  print(0)
  exit(0)
res = 1
t.reverse()
for x, _ in t[: K]:
  res *= x
  res %= mod
print(-res % mod)

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

P = []
M = []
Z = []
for i in range(N):
    if A[i]>0:
        P.append(A[i])
    elif A[i]<0:
        M.append(A[i])
    else:
        Z.append(A[i])

mod = 1000000007

if len(P)+len(M) < K:
    print(0)
elif len(P)==0:
    if K%2==1:
        if len(Z)>0:
            print(0)
        else:
            M.sort(reverse=True)
            ans = 1
            for i in range(K):
                ans*=abs(M[i])
                ans%=mod
            if ans==0:
                print(ans)
            else:
                print(mod-ans)
    else:
        M.sort()
        ans = 1
        for i in range(K):
            ans*=abs(M[i])
            ans%=mod
        print(ans)
else:
    B = [(abs(A[i]),A[i]) for i in range(N)]
    B.sort(reverse=True,key=lambda x:x[0])
    pc = 0
    lastpc = -1
    mc = 0
    lastmc = -1
    for i in range(K):
        if B[i][1]>0:
            pc += 1
            lastpc = i
        else:
            mc += 1
            lastmc = i
    if mc%2==0:
        ans = 1
        for i in range(K):
            ans *= B[i][0]
            ans %= mod
        print(ans)
    else:
        nextpc = -1
        nextmc = -1
        for i in range(K,N):
            if B[i][1]>0:
                if nextpc==-1:
                    nextpc=i
            elif B[i][1]<0:
                if nextmc==-1:
                    nextmc=i
        if nextmc==-1 and nextpc==-1:
            if len(Z)>0:
                print(0)
            else:
                ans = 1
                for i in range(K):
                    ans *= B[i][0]
                    ans %= mod
                if ans==0:
                    print(ans)
                else:
                    print(mod-ans)
        elif nextmc==-1:
            ans = 1
            for i in range(K):
                if i!=lastmc:
                    ans *= B[i][0]
                    ans %= mod
            ans *= B[nextpc][0]
            ans %= mod
            print(ans)
        elif nextpc==-1:
            ans = 1
            for i in range(K):
                if i!=lastpc:
                    ans *= B[i][0]
                    ans %= mod
            ans *= B[nextmc][0]
            ans %= mod
            print(ans)
        else:
            if lastpc==-1:
                ans = 1
                for i in range(K):
                    if i!=lastmc:
                        ans *= B[i][0]
                        ans %= mod
                ans *= B[nextpc][0]
                ans %= mod
                print(ans)
            elif lastmc==-1:
                ans = 1
                for i in range(K):
                    if i!=lastpc:
                        ans *= B[i][0]
                        ans %= mod
                ans *= B[nextmc][0]
                ans %= mod
                print(ans)
            elif B[nextpc][0]*B[lastpc][0] > B[nextmc][0]*B[lastmc][0]:
                ans = 1
                for i in range(K):
                    if i!=lastmc:
                        ans *= B[i][0]
                        ans %= mod
                ans *= B[nextpc][0]
                ans %= mod
                print(ans)
            else:
                ans = 1
                for i in range(K):
                    if i!=lastpc:
                        ans *= B[i][0]
                        ans %= mod
                ans *= B[nextmc][0]
                ans %= mod
                print(ans)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
MOD = 10 ** 9 + 7

plus = []
minus = []
zero = []
for i in range(n):
    if a[i] < 0:
        minus.append(abs(a[i]))
    elif a[i] > 0:
        plus.append(a[i])
    else:
        zero.append(0)


len_p = len(plus)
len_m = len(minus)
len_z = len(zero)

is_z = False
is_p = False
if len_z != 0:
    is_z = True

cnt = 2 * (len_m // 2) + len_p
if cnt >= k:
    is_p = True
if len_p == 0 and k % 2 == 1:
    is_p = False

if is_p:
    minus = sorted(minus, reverse=True)
    plus = sorted(plus, reverse=True)
    ans = 1
    if k % 2 == 1:
        k -= 1
        ans = plus[0]
        del plus[0]
    tmp = []
    for i in range(len(minus) // 2):
        tmp.append(minus[2 * i] * minus[2 * i + 1])
    for i in range(len(plus) // 2):
        tmp.append(plus[2 * i] * plus[2 * i + 1])
    tmp = sorted(tmp, reverse=True)
    for i in range(k // 2):
        ans *= tmp[i]
        ans %= MOD
    print(ans)
    exit()

elif is_z:
    print(0)
    exit()

else:
    for i in plus:
        minus.append(i)
    minus = sorted(minus)
    ans = -1
    for i in range(k):
        ans *= minus[i]
        ans %= MOD
    print(ans)
    exit()

Solution (Python):
n,k = map(int,input().split())
a = sorted(list(map(int,input().split())))
mod = 10**9 + 7
ans = 1

if n == k:
    for x in a:
        ans *= x
        ans %= mod
    print(ans)
    exit()

b = []
c = []
for x in a:
    if 0 <= x:
        b += [x]
    else:
        c += [x]

if len(b) == 0 and k % 2 == 1:
    c.reverse()
    for i in range(k):
        ans *= c[i]
        ans %= mod
    print(ans)
    exit()

b.reverse()
bi = 0
ci = 0
bl = len(b)
cl = len(c)
if k % 2 == 1:
    ans *= b[0]
    bi += 1
    k -= 1
while k > 0:
    if bl-1 <= bi:
        ans *= c[ci] * c[ci+1]
        ans %= mod
        ci += 2
    elif cl-1 <= ci:
        ans *= b[bi] * b[bi + 1]
        ans %= mod
        bi += 2
    else:
        if b[bi] * b[bi + 1] < c[ci] * c[ci + 1]:
            ans *= c[ci] * c[ci + 1]
            ans %= mod
            ci += 2
        else:
            ans *= b[bi] * b[bi + 1]
            ans %= mod
            bi += 2
    k -= 2
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
mod = 10**9+7

n, k = map(int, input().split())
A = list(map(int, input().split()))
B = []
C = []
s = 0
t = 0
for a in A:
  if a < 0:
    B.append(-a)
    s += 1
  elif a > 0:
    C.append(a)
    t += 1
if s + t < k:
  print(0)
  exit()
if s + t == k:
  if k == n:
    ans = 1
    for a in A:
      ans *= a
      ans %= mod
    print(ans)
  else:
    if s%2:
      print(0)
    else:
      ans = 1
      for b in B:
        ans *= b
        ans %= mod
      for c in C:
        ans *= c
        ans %= mod
      print(ans)
  exit()
B.sort(reverse=True)
C.sort(reverse=True)
ans = 1
nb = 0
nc = 0
if t == 0:
  if k%2:
    if s == n:
      for b in B[-k:]:
        ans *= b
        ans %= mod
      ans *= -1
      ans %= mod
      print(ans)
    else:
      print(0)
  else:
    for b in B[:k]:
      ans *= b
      ans %= mod
    ans %= mod
    print(ans)
  exit()
for _ in range(k):
  if nb >= s:
    ans *= C[nc]
    ans %= mod
    nc += 1
  elif nc >= t:
    ans *= B[nb]
    ans %= mod
    nb += 1
  else:
    if B[nb] > C[nc]:
      ans *= B[nb]
      ans %= mod
      nb += 1
    else:
      ans *= C[nc]
      ans %= mod
      nc += 1
if nb%2:
  if nb == s:
    ans *= pow(B[-1], mod-2, mod)
    ans %= mod
    ans *= C[nc]
    ans %= mod
    print(ans)
  elif nc == t:
    ans *= B[nb]
    ans %= mod
    ans *= pow(C[-1], mod-2, mod)
    ans %= mod
    print(ans)
  else:
    if nc == 0:
      ans *= pow(B[nb-1], mod-2, mod)
      ans %= mod
      ans *= C[0]
      ans %= mod
      print(ans)
    else:
      if C[nc-1]*C[nc] >= B[nb-1]*B[nb]:
        ans *= pow(B[nb-1], mod-2, mod)
        ans %= mod
        ans *= C[nc]
        ans %= mod
        print(ans)
      else:
        ans *= pow(C[nc-1], mod-2, mod)
        ans %= mod
        ans *= B[nb]
        ans %= mod
        print(ans)
else:
  print(ans)

Solution (Python):
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, K = map(int, readline().split())
A = np.array(read().split(), np.int64)

def f(A, N, K):
    A.sort()
    I = (np.abs(A)).argsort()
    A = A[I][::-1]
    if N == K:
        return A
    
    if np.all(A < 0):
        if K % 2 == 0:
            return A[:K]
        else:
            return A[-K:]
    
    pos = A[A >= 0]
    neg = A[A < 0]
    nums = [1]
    if K & 1:
        nums = [pos[0]]
        pos = pos[1:]
        K -= 1
    
    
    if len(pos) & 1:
        pos = pos[:-1]
    if len(neg) & 1:
        neg = neg[:-1]
    pos = pos[::2] * pos[1::2]
    neg = neg[::2] * neg[1::2]
    A = np.concatenate([pos, neg])
    A.sort()
    A = A[::-1]
    return np.concatenate([nums, A[:K // 2]])

MOD = 10**9 + 7
B = f(A, N, K).tolist()
ans = 1
for x in B:
    ans = ans * x % MOD
print(ans)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7

positives = [a for a in A if a > 0]
negatives = [a for a in A if a < 0]
positive_cnt = len(positives)
negative_cnt = len(negatives)
zero_cnt = A.count(0)


if min(2 * (negative_cnt // 2), 2 * (K // 2)) + positive_cnt >= K:
    
    positives = sorted(positives, reverse=True)
    negatives = sorted(negatives)

    positives_twin = [positives[2 * i] * positives[2 * i + 1] for i in range(positive_cnt // 2)] + [0]
    negatives_twin = [negatives[2 * i] * negatives[2 * i + 1] for i in range(negative_cnt // 2)] + [0]
    positives_twin.sort()
    negatives_twin.sort()

    ans = 1
    positive_select = 0
    for i in range(K // 2):
        if positives_twin[-1] <= negatives_twin[-1]:
            ans *= negatives_twin[-1]
            ans %= MOD
            negatives_twin.pop()
        else:
            ans *= positives_twin[-1]
            ans %= MOD
            positives_twin.pop()
            positive_select += 1

    if K % 2 == 1:
        if K == 1:
            ans = positives[2 * positive_select]
        else:
            ans *= positives[2 * positive_select]
    print(ans % MOD)

elif zero_cnt > 0:
    
    print(0)
else:
    
    ans = 1
    A_abs = sorted([abs(a) for a in A])
    for a in A_abs[:K]:
        ans *= a
        ans %= MOD
    ans = -ans % MOD
    print(ans)

Solution (Python):
mod = 10**9 + 7

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

pos_cnt = sum(ai>=0 for ai in a)
neg_cnt = n-pos_cnt
if k-pos_cnt > 0 and (k-pos_cnt)%2 == 1 and (n==k or pos_cnt == 0):
    res = 1
    a.sort(reverse=True)
    for i in range(k):
        res *= a[i]
        res %= mod
    print(res)
    exit()

b = [(abs(ai), ai) for ai in a]
b.sort(reverse=True)
pos_top = []
neg_top = []
for i in range(k):
    _, ai = b[i]
    if ai >= 0:
        pos_top.append(ai)
    else:
        neg_top.append(ai)

pos_left = []
neg_left = []
for i in range(k, n):
    _, ai = b[i]
    if ai >= 0:
        pos_left.append(ai)
    else:
        neg_left.append(ai)

ans = 1
if len(neg_top)%2 == 0:
    for ai in pos_top:
        ans *= ai
        ans %= mod
    for ai in neg_top:
        ans *= ai
        ans %= mod
    print(ans)
    exit()

ans_tmp = []
if len(pos_left) > 0 and len(neg_top) > 0:
    pos_use = pos_left[0]
    neg_drop = neg_top[-1]
    pos_tmp = pos_top[:]
    pos_tmp.append(pos_use)
    neg_tmp = neg_top[:]
    neg_tmp.pop()
    res = 1
    for ai in pos_tmp:
        res *= ai
        res %= mod
    for ai in neg_tmp:
        res *= ai
        res %= mod
    ans_tmp.append([abs(pos_use/neg_drop), res])

if len(neg_left) > 0 and len(pos_top) > 0:
    neg_use = neg_left[0]
    pos_drop = pos_top[-1]
    pos_tmp = pos_top[:]
    pos_tmp.pop()
    neg_tmp = neg_top[:]
    neg_tmp.append(neg_use)
    res = 1
    for ai in pos_tmp:
        res *= ai
        res %= mod
    for ai in neg_tmp:
        res *= ai
        res %= mod
    ans_tmp.append([abs(neg_use/pos_drop), res])

ans_tmp.sort(reverse=True)
ans = ans_tmp[0][1]
print(ans)

Solution (Python):
from bisect import bisect_left

MOD = 10 ** 9 + 7

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

A.sort()

n = bisect_left(A, 0) 
p = N - bisect_left(A, 0.5)
z = N - n - p

if K == 1: 
    print (A[-1] % MOD)
    exit()

if n + p < K: 
    print (0)
    exit()

i = 0 
j = N - 1 

if p >= 1: 
    if K % 2 == 0: 
        ans = 1
    else:
        ans = A[-1]
        j -= 1
        K -= 1
    
    k = K // 2
    for _ in range(k):
        if A[i] * A[i + 1] < A[j - 1] * A[j]:
            ans = (((ans * A[j - 1]) % MOD) * A[j]) % MOD
            j -= 2
        else:
            ans = (((ans * A[i]) % MOD) * A[i + 1]) % MOD
            i += 2
else:
    if K % 2 == 0: 
        ans = 1
        for i in range(K):
            ans = (ans * A[i]) % MOD
    else: 
        ans = 1
        for i in range(K):
            ans = (ans * (A[N - 1 - i])) % MOD

print (ans % MOD)

Solution (Python):
n,k = map(int,input().split())
A = list(map(int,input().split()))

mod = 10**9 + 7

A.sort(key = lambda x:abs(x),reverse = True)
ans = 1
last = -1
lastp = -1
cnt = 0
for a in A:
    if a >= 0:break
else:
    if k&1:
        for i in range(k):
            ans *= A[n-i-1]
            ans %= mod
    else:
        for i in range(k):
            ans *= A[i]
            ans %= mod
    print(ans)
    exit()
for i in range(k):
    ans *= A[i]
    ans %= mod
    if A[i] < 0:
        last = i
        cnt += 1
    if A[i] > 0:
        lastp = i
if n == k:
    print(ans%mod)
    exit()
if cnt&1:
    first = 0
    firstp = 0
    for a in A[k:]:
        if a > 0 and firstp == 0:
            firstp = a
        if a < 0 and first == 0:
            first = a
    if first == 0 and firstp == 0:
        ans = 0
    elif first == 0 or lastp == -1:
        ans *= pow(A[last],mod-2,mod)*firstp%mod
        ans %= mod
    elif firstp == 0:
        ans *= pow(A[lastp],mod-2,mod)*first%mod
        ans %= mod
    else:
        if A[lastp]*firstp <= A[last]*first:
            ans *= pow(A[lastp],mod-2,mod)*first%mod
            ans %= mod
        else:
            ans *= pow(A[last],mod-2,mod)*firstp%mod
            ans %= mod
print(ans%mod)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
pos = []
neg = []
for v in A:
	if v >= 0: pos.append(v)
	else: neg.append(-v)

pos.sort(reverse=True)
neg.sort(reverse=True)

MOD = 10 ** 9 + 7
ppos = [1] * (len(pos)+1)
pneg = [1] * (len(neg)+1)

for i, v in enumerate(pos):
	ppos[i+1] = ppos[i] * v % MOD
for i, v in enumerate(neg):
	pneg[i+1] = pneg[i] * v % MOD

i = j = 0

while i + j < K:
	if i == len(pos): j += 1
	elif j == len(neg) or pos[i] >= neg[j]: i += 1
	else: j += 1

if j % 2 == 1:
	v = (-1, -1, -1)
	if i and j < len(neg):
		v = (neg[j-1] * neg[j], i-1, j+1)

	if i < len(pos) and j:
		v = max(v, (pos[i-1] * pos[i], i+1, j-1))

	if v[0] == -1:
		X = pos + neg
		X.sort()
		r = 1
		for v in X[:K]: r = r * v % MOD
		print(-r % MOD)
		raise SystemExit

	i, j = v[1:]

r = 1
for x in pos[:i]: r = r * x % MOD
for x in neg[:j]: r = r * x % MOD
print(r)

Solution (Python):
MOD = 10 ** 9 + 7
it = lambda: list(map(int, input().strip().split()))

def solve():
    N, K = it()
    A = it()
    
    zero = 0
    neg = []
    pos = []
    for i in range(N):
        if A[i] < 0:
            neg.append(A[i])
        elif A[i] > 0:
            pos.append(A[i])
        else:
            zero += 1
    
    if not neg and not pos:
        return 0
    
    pos.sort()
    neg.sort(reverse=True)

    if not pos and K % 2 == 1:
        if zero > 0:
            return 0

        ans = 1
        for i in range(K):
            ans = ans * neg[i] % MOD
        return ans

    ans = 1
    for _ in range(K >> 1):
        if len(pos) >= 2 and len(neg) >= 2:
            if neg[-1] * neg[-2] >= pos[-1] * pos[-2]:
                ans = ans * neg.pop() % MOD * neg.pop() % MOD
            else:
                ans = ans * pos.pop() % MOD * pos.pop() % MOD
        elif len(pos) >= 2:
            ans = ans * pos.pop() % MOD * pos.pop() % MOD
        elif len(neg) >= 2:
            ans = ans * neg.pop() % MOD * neg.pop() % MOD
        elif len(pos) == 1 and len(neg) == 1:
            ans = ans * pos.pop() % MOD * neg.pop() % MOD
        else:
            return 0
    
    if K % 2 == 1:
        if pos:
            ans = ans * pos.pop() % MOD
        elif neg:
            ans = ans * neg.pop() % MOD
        else:
            return 0
    if zero > 1 and ans < 0:
        return 0
    return ans


if __name__ == '__main__':
    print(solve())

Solution (Python):
N,K = map(int,input().split())
*A, = map(int,input().split())

B = [a for a in A if a>=0]
B.sort()
C = [a for a in A if a<0]
C.sort(reverse = True)

ans = 1
MOD = 10**9+7

if len(B)==0 and K%2==1:
    C.sort()
    for _ in [0]*K:
        ans *= C.pop()
        ans %= MOD
    print(ans)
    exit()

if K==N:
    for a in A:
        ans = ans * a % MOD
    print(ans)
    exit()

while K>=2:
    if len(B)>=2 and len(C)>=2:
        if B[-1]*B[-2] > C[-1]*C[-2]:
            ans *= B.pop()*B.pop()%MOD
            ans %= MOD
            K -= 2
        else:
            ans *= C.pop()*C.pop()%MOD
            ans %= MOD
            K -= 2
    elif len(C)>=2:
        ans *= C.pop()*C.pop()%MOD
        ans %= MOD
        K -= 2
    else:
        ans *= B.pop()
        ans %= MOD
        K -=1
if K==1:
    ans *= B.pop()
    ans %= MOD

ans %= MOD
print(ans)

Solution (Python):
import sys

n, k = map(int, input().split())
a = [int(x) for x in input().split()]
mod = pow(10, 9)+7
zero = 0
plus = []
minus = []
for i in range(n):
  if a[i] == 0:
    zero += 1
  elif a[i] > 0:
    plus.append(a[i])
  else:
    minus.append(a[i])

P, M = len(plus), len(minus)
plus.sort(reverse=True)
minus.sort()
ans = 1
if n == k:
  for i in range(n):
    ans *= a[i]
    ans %= mod
elif P+M < k:
  ans = 0
elif P == 0:
  if zero and k%2:
    ans = 0
  else:
    if k%2:
      minus.reverse()
    for i in range(k):
      ans *= minus[i]
      ans %= mod
else:
  q = 0
  if k%2:
    ans *= plus[0]
    ans %= mod
    q = 1
  judge = []
  for i in range((len(plus)-q)//2):
    judge.append(plus[2*i+q]*plus[2*i+q+1])
  for i in range(len(minus)//2):
    judge.append(minus[2*i]*minus[2*i+1])
  judge.sort(reverse=True)
  for i in range(k//2):
    ans *= (judge[i]%mod)
    ans %= mod

print(ans)

Solution (Python):
n, k = map(int, input().split())
mod = 10 ** 9 + 7

from collections import deque
a = list(map(int, input().split()))
a.sort()
minus, zero, plus = 0, 0, 0
for i in range(n):
    if a[i] < 0:
        minus += 1
    elif a[i] == 0:
        zero += 1
    else:
        plus += 1

que = deque(a)

plus_flg = False
if plus >= k:
    plus_flg = True
for i in range(plus+1):
    if (k - i) % 2 == 0 and (k - i) // 2 <= minus // 2:
        plus_flg = True
        break

if plus_flg:
    ans = 1
    cnt = 0 
    
    while cnt < k:
        if k - cnt >= 2:
            cnt += 2
            if que[0] * que[1] >= que[-1] * que[-2]:
                ans *= que.popleft()
                ans *= que.popleft()
                ans %= mod
            else:
                ans *= que.pop()
                ans *= que.pop()
                ans %= mod
        else:
            ans *= que.pop()
            ans %= mod
            cnt += 1

    print(ans)




elif zero > 0:
    print(0)


else:
    a = [abs(a[i]) for i in range(n)]
    a.sort()
    ans = 1
    for i in range(k):
        ans *= a[i]
        ans %= mod
    ans = -ans % mod
    print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()

n, k = na()
a = na()

neg = []
pos = []
for i in range(n):
    if a[i] < 0:
        neg.append(a[i])
    elif a[i] > 0:
        pos.append(a[i])
neg.sort()
pos.sort(reverse=True)

mx = -1
arg = -1
lall = 0

nn, np = 0, k
while np > len(pos):
    np -= 2
    nn += 2

if np >= 0 and nn <= len(neg):
    arg = nn
    while np-2 >= 0 and nn+1 < len(neg):
        if pos[np-1] * pos[np-2] < neg[nn] * neg[nn+1]:
            nn += 2
            np -= 2
            arg = nn
        else:
            break

ans = 1
mod = 1000000007
if arg == -1:
    a.sort(reverse=True)
    for i in range(k):
        ans = ans * a[i] % mod
    print(ans)
else:
    for i in range(arg):
        ans = ans * neg[i] % mod
    for i in range(k-arg):
        ans = ans * pos[i] % mod
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

N, K = [int(_) for _ in input().split()]

A = [int(_) for _ in input().split()]
MOD = 10**9 + 7

Apos = sorted([el for el in A if el > 0], reverse=True)
Aneg = sorted([el for el in A if el <= 0])

Cpos = 0
Cneg = 0

answer = 1


if K % 2 and len(Apos) == 0:
    Aneg = Aneg[::-1]
    for i in range(K):
        answer *= Aneg[i]
        answer %= MOD
else:
    while K > 0:
        if K % 2:
            if Cpos < len(Apos):
                answer *= Apos[Cpos]
                Cpos += 1
            else:
                answer *= Aneg[Cneg]
                Cneg += 1
            answer %= MOD
            K -= 1
        else:
            if Cpos + 1 >= len(Apos) and Cneg + 1 >= len(Aneg):
                answer *= Apos[Cpos] * Aneg[Cneg]
                Cpos += 1
                Cneg += 1
            elif Cpos + 1 >= len(Apos):
                answer *= Aneg[Cneg] * Aneg[Cneg + 1]
                Cneg += 2
            elif Cneg + 1 >= len(Aneg):
                answer *= Apos[Cpos] * Apos[Cpos + 1]
                Cpos += 2
            else:
                if Apos[Cpos] * Apos[Cpos+1] > Aneg[Cneg] * Aneg[Cneg+1]:
                    answer *= Apos[Cpos] * Apos[Cpos + 1]
                    Cpos += 2
                else:
                    answer *= Aneg[Cneg] * Aneg[Cneg + 1]
                    Cneg += 2
            K -= 2
            answer %= MOD
print(answer)

Solution (Python):
import sys


def solve(n, k, aaa):
    pos = []
    neg = []
    MOD = 10 ** 9 + 7
    for a in aaa:
        if a > 0:
            pos.append(a)
        elif a < 0:
            neg.append(a)
    if len(pos) + len(neg) < k:
        return 0
    if len(pos) == 0:
        if k % 2 == 1:
            if len(neg) < n:
                return 0
            neg.sort(reverse=True)
        else:
            neg.sort()
        ans = 1
        for a in neg[:k]:
            ans = ans * a % MOD
        return ans
    if len(pos) + len(neg) == k:
        if n > k and len(neg) % 2 == 1:
            return 0
        ans = 1
        for a in pos:
            ans = ans * a % MOD
        for a in neg:
            ans = ans * a % MOD
        return ans

    pos.sort()
    neg.sort(reverse=True)

    ans = 1
    r = k
    while r >= 2:
        if len(pos) <= 1:
            ans = ans * neg.pop() * neg.pop() % MOD
        elif len(neg) <= 1:
            ans = ans * pos.pop() * pos.pop() % MOD
        else:
            pm = pos[-1] * pos[-2]
            nm = neg[-1] * neg[-2]
            if pm > nm:
                ans = ans * pm % MOD
                pos.pop()
                pos.pop()
            else:
                ans = ans * nm % MOD
                neg.pop()
                neg.pop()
        r -= 2

    if r == 1:
        ans = ans * pos[-1] % MOD

    return ans


n, k, *aaa = map(int, sys.stdin.buffer.read().split())
print(solve(n, k, aaa))

Solution (Python):
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)