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 C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
#define SORT(A) std::sort(A.begin(), A.end())
#define RSORT(A) std::sort(A.begin(), A.end(), std::greater<>())
#define FR(A, e) for (auto& e : A)
#define SHOW(A) for (auto& e : A) {cout << e << " ";}; cout << endl;
#define within(x,_left,_right) (_left <= x && x <= _right)
#define rwithin(x, _l, _r) (_l < x && x < _r)
#define ALL(A) A.begin(),A.end()
#define RALL(A) (A).rbegin(),(A).rend()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define sz(x) ((int)x.size())
#define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end())
#define bitcnt(x) (__builtin_popcountll(x))
#define fi first
#define se second
#define bg(A) A.begin()
#define ed(A) A.end()
#define rbg(A) A.rbegin()
#define red(A) A.rend()
using LL = long long;
using db = double;
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using VL = vector<LL>;
using VVL = vector<VL>;
const LL MOD = 1e9 + 7;
const LL LMAX = 0x7fffffffffffffff;
const LL LMIN = 0x8000000000000000;
typedef std::pair<int, int> PII;
typedef std::pair<LL, LL> PLL;
typedef std::pair<int, char> PIC;
// int dx[4] = {1, -1, 0, 0}; // 冯诺依曼型邻居
// int dy[4] = {0, 0, 1, -1};
// int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; // 摩尔型邻居
// int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
// #include<iomanip> cout << fixed <<  setprecision(k) 输出k位精度

int N, K;
VL pos, neg, zero;

void mul(LL& a, LL b){
    a = a*b % MOD;
}

int main(){
    std::cin >> N >> K;

    pos.clear(), neg.clear(), zero.clear();

    int t;
    for (int i = 0; i < N; ++ i){
        cin >> t;
        if (t > 0) pos.emplace_back(t);
        else if (t == 0) zero.emplace_back(t);
        else neg.emplace_back(t);
    }

    RSORT(pos);
    SORT(neg);

    int a = sz(pos), b = sz(neg), c = sz(zero);
    LL res = 1;

    if (a + b < K){
        cout << 0 << endl;
        return 0;
    }

    if (a + b == K){
        if ((b & 1) && c != 0){
            cout << 0 << endl;
        }else {
            for (auto &x: pos) res = (res * 1LL * x) % MOD;
            for (auto &x: neg) res = ((res * 1LL * x) + MOD) % MOD;
            cout << (res + MOD) % MOD << endl;
        }
        return 0;
    }

    int pp = 0, pn = 0;
    if ((K & 1) && a != 0){
        res = (1LL * res * pos[pp++]) % MOD;
        -- K;
    }
    else if ((K & 1) && a == 0 && c != 0){
        cout << 0 << endl;
        return 0;
    }else if ((K & 1) && a == 0 && c == 0){
        for (int i = 0, x = 0; i < K; ++ i) x = neg[b - 1 - i], res = ((res * 1LL * x) + MOD) % MOD;
        cout << (res + MOD) % MOD << endl;
        return 0;
    }

    while (K){
        LL pmax = 0, nmax = 0;
        if (pp + 1 < a) pmax = pos[pp] * pos[pp + 1] % MOD;
        if (pn + 1 < b) nmax = neg[pn] * neg[pn + 1] % MOD;

        if (pmax >= nmax){
            // res = (1LL * res * pmax) % MOD;
            mul(res, pmax % MOD);
            pp += 2;
        }else {
            // res = (1LL * res * nmax) % MOD;
            mul(res, nmax % MOD);
            pn += 2;
        }
        K -= 2;
    }

    cout << (res + MOD) % MOD << endl;

    return 0;
}