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):
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort(key=abs, reverse=1)

mod = 10**9+7
ans = 1
sign = 1
for i in range(k):
    ans *= a[i]
    ans %= mod
    if a[i] < 0:
        sign *= -1
    elif a[i] == 0:
        sign = 0
if sign >= 0:
    print(ans)
else:
    t = None
    ok = True
    for i in reversed(range(k)):
        if a[i] < 0:
            break
    else:
        ok = False
    for j in range(k, n):
        if a[j] > 0:
            break
    else:
        ok = False

    if ok:
        t = (a[i], a[j])

    ok = True
    for i in reversed(range(k)):
        if a[i] > 0:
            break
    else:
        ok = False
    for j in range(k, n):
        if a[j] < 0:
            break
    else:
        ok = False

    if ok:
        if t is None:
            t = (a[i], a[j])
        else:
            if (t[1]*a[i] - t[0]*a[j]) * t[0]*a[j] < 0:
                t = (a[i], a[j])

    if t is None:
        ans = 1
        a.reverse()
        for i in range(k):
            ans *= a[i]
            ans %= mod
        print(ans)
    else:
        print(ans*t[1] * pow(t[0], mod-2, mod) % mod)

Solution (Python):
N, K = map(int, input().split())
A = sorted([int(a) for a in input().split()], key = lambda x: -abs(x))

P = 10 ** 9 + 7
m = 0
ans = 1
lp = 0
lm = 0
np = 0
nm = 0
for a in A[:K]:
    if a > 0:
        lp = a
        ans = ans * a % P
    elif a < 0:
        lm = a
        m ^= 1
        ans = ans * a % P
    else:
        print(0)
        exit()

if m == 0:
    print(ans)
    exit()

for a in A[K:]:
    if a > 0:
        np = a
        break
for a in A[K:]:
    if a < 0:
        nm = a
        break

if lp and nm and lm and np:
    if lp * np > lm * nm:
        print(ans * pow(lm, P-2, P) * np % P)
    else:
        print(ans * pow(lp, P-2, P) * nm % P)
elif lp and nm:
    print(ans * pow(lp, P-2, P) * nm % P)
elif lm and np:
    print(ans * pow(lm, P-2, P) * np % P)
elif A[-1] == 0:
    print(0)
else:
    A = A[::-1][:K]
    ans = 1
    for a in A:
        ans = ans * a % P
    print(ans)

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

N, K = map(int, input().split())
A = list(map(int, input().split()))
pos = []
neg = []
for a in A:
    if a >= 0:
        pos.append(a)
    else:
        neg.append(-a)
pos = deque(sorted(pos))
neg = deque(sorted(neg))
mod = 10**9 + 7
ans = 1
sign = 1
if K % 2 == 1:
    if len(pos) > 0:
        ans = ans * pos.pop() % mod
    else:
        ans = ans * neg.popleft() % mod
        sign *= -1
    K -= 1
while K > 0:
    if len(pos) == 1 and len(neg) == 1:
        ans = ans * -neg.pop() * pos.pop() % mod
    elif sign == 1:
        if len(pos) < 2 or (len(neg) >= 2 and pos[-2] * pos[-1] < neg[-2] * neg[-1]):
            ans = ans * neg.pop() * neg.pop() % mod
        else:
            ans = ans * pos.pop() * pos.pop() % mod
    else:
        if len(pos) < 2 or (len(neg) >= 2 and pos[-2] * pos[-1] < neg[-2] * neg[-1]):
            ans = ans * neg.popleft() * neg.popleft() % mod
        else:
            ans = ans * pos.popleft() * pos.popleft() % mod
    K -= 2
ans = ans * sign % mod
print(ans)