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 Python (3.8.2) (judge verdict: Runtime Error)

x = list(input().split())
a = list((int(i) for i in input().split()))
n, k = int(x[0]), int(x[1])
#import random
#n = random.choice(range(10000))
#k = random.choice(range(n))
#a = random.choices(range(-1000000, 1000000, 1), k=n)
#print('n =', n, 'k =', k)

MOD = 10**9 + 7

a_abs = []
for i in range(len(a)): a_abs.append(abs(a[i]))

res = sorted(list(zip(a_abs, a)))
val = []
for i in range(len(res)): val.append(res.pop()[1])

# check negative mode
signA, signB, neg = True, False, False
for i in range(len(val)):
    if val[i] < 0:
        signA = not signA
    else:
        signB = True
if (n==k and not signA) or (not signB):
    neg = True # answer will be negative

index_replaced, index_replace = None, None
index_replaced_a, index_replace_a = None, None
if (not neg): # positive
    # take top k
    ans = val[0:k]
    sign = True
    for i in range(len(ans)):
        if ans[i] < 0:
            sign = not sign
            
    # replace if necessary
    if (not sign):
        # find smallest negative
        for i in range(len(ans), 0, -1):
            if ans[i-1] < 0:
                index_replaced = i-1
                break
        # find largest positive
        for i in range(k, len(val)):
            if val[i] > 0:
                index_replace = i
                break
        # find smallest positive
        for i in range(len(ans), 0, -1):
            if ans[i-1] > 0:
                index_replaced_a = i-1
                break
        # find largest negative
        for i in range(k, len(val)):
            if val[i] < 0:
                index_replace_a = i
                break

        if index_replaced==None or index_replace==None:
            ans[index_replaced_a] = val[index_replace_a]
        elif index_replaced_a==None or index_replace_a==None:
            ans[index_replaced] = val[index_replace]
        elif (ans[index_replaced]/val[index_replace] > 
            ans[index_replaced_a]/val[index_replace_a]):
            ans[index_replaced] = val[index_replace]
        else:
            ans[index_replaced_a] = val[index_replace_a]

    # multiplication
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)

else: # negative case
    ans = val[len(val)-k:]
    result = 1
    for i in range(len(ans)):
        result = (result * ans[i]) % MOD

    print(result)