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)

import math


def can_plus(PLUSS, MINUSS, K):
    PLUSS = list(reversed(sorted(PLUSS)))
    MINUSS = list(sorted(MINUSS))

    cumul = 1

    plus_idx = 0
    minus_idx = 0

    cnt = 0
    while True:
        if cnt == K:
            break
        elif cnt == K-1:
            cumul = cumul * PLUSS[plus_idx]
            plus_idx += 1
            cnt += 1
        elif (plus_idx <= len(PLUSS) - 3) and (minus_idx <= len(MINUSS) - 2):
            plus_pair = PLUSS[plus_idx] * PLUSS[plus_idx + 1]
            minus_pair = MINUSS[minus_idx] * MINUSS[minus_idx + 1]
            if plus_pair >= minus_pair:
                cumul = cumul * plus_pair
                plus_idx += 2
            else:
                cumul = cumul * minus_pair
                minus_idx += 2
            cnt += 2
        elif minus_idx >= len(MINUSS) - 1:
            cumul = cumul * PLUSS[plus_idx]
            plus_idx += 1
            cnt += 1
        else:
            cumul = cumul * MINUSS[minus_idx] * MINUSS[minus_idx + 1]
            minus_idx += 2
            cnt += 2
    return cumul


def main():
    a = input()
    aa = a.split()
    K = int(aa[1])

    b = input()
    A = [int(bb) for bb in b.split()]

    PLUSS = []
    MINUSS = []
    ZEROS = []
    for a in A:
        if a > 0:
            PLUSS.append(a)
        elif a < 0:
            MINUSS.append(a)
        else:
            ZEROS.append(a)

    # ゼロ以下になることが避けられない場合
    if len(MINUSS) + len(PLUSS) < K:
        print("0")
        return
    elif (len(PLUSS) == 0 or (len(MINUSS) + len(PLUSS) == K)) and (len(MINUSS) % 2) == 1:
        if len(ZEROS) > 0:
            print("0")
            return
        elif len(PLUSS) == 0:
            cumul = 1
            m = list(reversed(sorted(MINUSS)))
            for i in range(K):
                cumul = cumul*m[i]
        else:
            cumul = 1
            for a in A:
                cumul = cumul * a
    else:
        cumul = can_plus(PLUSS, MINUSS, K)

    DIV = 1_000_000_007
    print(cumul % DIV)


main()