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 in PyPy3 (7.3.0) (judge verdict: Accepted)

n,k = map(int,input().split())
a = list(map(int,input().split()))
p = []
m = []
mod = 10**9 + 7
if n == k:
  ans = 1
  for x in a:
    ans *= x
    ans %= mod
  print(ans)
  exit()
zero = 0
for x in a:
  if x == 0:
    zero += 1
  if x > 0:
    p.append(x)
  else:
    m.append(-x)
p.sort(reverse=True)
m.sort(reverse=True)
#print(zero,p,m)
nowp = 0
nowm = 0
lenp = len(p)
lenm = len(m)
if k + zero > n:
  print(0)
  exit()
n -= zero
ans = 1
def inv(x):
  y = 1
  while x != 1:
    y *= mod // x + 1
    y %= mod
    x -= mod % x
  return y
pp = 0
for _ in range(k):
  if nowp == lenp:
    ans *= m[nowm]
    ans *= -1
    ans %= mod
    nowm += 1
    pp = 1-pp
  elif nowm == lenm:
    ans *= p[nowp]
    ans %= mod
    nowp += 1
  elif p[nowp] >= m[nowm]:
    ans *= p[nowp]
    ans %= mod
    nowp += 1
    #print('$$')
  else:
    ans *= m[nowm]
    ans *= -1
    ans %= mod
    nowm += 1
    pp = 1-pp
    #print('$')
  #print(pp,nowp,nowm)
#print(pp,ans)
if pp == 0:
  print(ans)
  exit()
if lenm == 0:
  print(ans)
  exit()
if lenp == 0:
  ans = 1
  m.reverse()
  for d in range(k):
    ans *= -m[d]
    ans %= mod
  print(ans)
  exit()
ans *= -1
ans %= mod
if lenp == nowp:
  ans *= inv(p[-1])
  ans *= m[nowm]
  ans %= mod
  print(ans)
  exit()
if lenm == nowm:
  ans *= inv(m[-1])
  ans *= p[nowp]
  ans %= mod
  print(ans)
  exit()
if nowp == 0:
  ans *= inv(m[nowm-1])
  ans *= p[0]
  ans %= mod
  print(ans)
  exit()
if nowm == 0:
  ans *= inv(p[nowp-1])
  ans *= m[0]
  ans %= mod
  print(ans)
  exit()
pl = p[nowp-1]
pr = p[nowp]
ml = m[nowm-1]
mr = m[nowm]
if mr * ml > pl * pr:
  ans *= inv(pl)
  ans *= mr
  ans %= mod
  print(ans)
else:
  ans *= inv(ml)
  ans *= pr
  ans %= mod
  print(ans)