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 PHP (7.4.4) (judge verdict: Wrong Answer)

<?php
define('MOD', 10 ** 9 + 7);
list($n, $k) = ints();
$a = ints();
$ap = $am = $az = [];
for ($i = 0; $i < $n; ++$i) {
    if ($a[$i] > 0) $ap[] = $a[$i];
    elseif ($a[$i] < 0) $am[] = -$a[$i];
    else $az[] = $a[$i];
}
// 0を選ぶしかない場合
if (count($az) > 1 && $k > count($ap) + count($am)) exit('0');
// マイナスを奇数個選ぶしかない場合
if (($k === count($ap) + count($am) && count($am) % 2) ||
    ($n === count($am) && $k % 2)
) {
    if (count($az) > 0) {
        exit('0');
    }
    $apm = array_merge($ap, $am);
    sort($apm);
    $ans = 1;
    for ($i = 0; $i < $k; ++$i) {
        $ans = modMul($ans, $apm[$i]);
    }
    echo modSub(0, $ans);
    exit;
}
rsort($ap);
rsort($am);
$q = new SplPriorityQueue();
for ($i = 1; $i < count($ap); $i += 2) {
    $q->insert([$ap[$i] * $ap[$i - 1], true], $ap[$i] * $ap[$i - 1]);
}
for ($i = 1; $i < count($am); $i += 2) {
    $q->insert([$am[$i] * $am[$i - 1], false], $am[$i] * $am[$i - 1]);
}
$i = 0;
$ans = 1;
while ($k > 1) {
    list($v, $f) = $q->extract();
    $k -= 2;
    $ans = modMul($ans, $v % MOD);
    if ($f) $i += 2;
}
if ($k === 1) $ans = modMul($ans, $ap[$i]);
echo $ans;
function ints()
{
    return array_map('intval', explode(' ', trim(fgets(STDIN))));
}
// 足し算
function modAdd($x, $y)
{
    return ($x + $y) % MOD;
}

// 引き算
function modSub($x, $y)
{
    return ($x + MOD - $y) % MOD;
}

// 掛け算
function modMul($x, $y)
{
    return ($x * $y) % MOD;
}

// 割り算
function modDiv($x, $y)
{
    return modMul($x, modPow($y, MOD - 2));
}

// 累乗（繰り返し二乗法）
function modPow($n, $x)
{
    if ($x === 0) return 1;
    $res = (modPow($n, $x >> 1) ** 2) % MOD;
    if ($x % 2 === 1) $res = modMul($res, $n);
    return $res;
}

// 階乗
function modFac($n)
{
    if ($n === 0) return 1;
    return modMul($n, modFac($n - 1));
}

// 順列
function nPr($n, $r)
{
    if ($r === 0) return 1;
    return modMul(nPr($n, $r - 1), $n - $r + 1);
}

// 組み合わせ
function nCr($n, $r)
{
    $r = min($r, $n - $r);
    if ($r === 0) return 1;
    return modDiv(nPr($n, $r), modFac($r));
}

// 階乗、逆元を前処理
function init($n)
{
    global $fact, $ifact;
    $fact[0] = 1;
    for ($i = 1; $i <= $n; ++$i) $fact[$i] = modMul($fact[$i - 1], $i);
    $ifact[$n] = modDiv(1, $fact[$n]);
    for ($i = $n; $i >= 1; --$i)
        $ifact[$i - 1] = modMul($ifact[$i], $i);
}

// 順列（前処理）
function nPr2($n, $r)
{
    global $fact, $ifact;
    return modMul($fact[$n], $ifact[$n - $r]);
}

// 組み合わせ（前処理）
function nCr2($n, $r)
{
    global $fact, $ifact;
    return modMul(modMul($fact[$n], $ifact[$r]), $ifact[$n - $r]);
}