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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll,ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll,ll>>>
#define PL pair<ll,ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main(){
    const ll mod = 1e9 + 7;
    ll n, k;
    cin >> n >> k;
    ll ek = (k / 2) * 2;
    VL a(n), neg, pos;
    ll zero = 0;
    rep(i, 0, n){
        cin >> a[i];
        if(a[i] == 0) zero++;
        else if(a[i] > 0) pos.push_back(a[i]);
        else neg.push_back(a[i]);
    }
    sort(ALL(pos), greater<ll>());
    sort(ALL(neg));
    priority_queue<ll> que;
    ll pn = pos.size(), nn = neg.size();

    if(k == n){
        ll ans = 1;
        rep(i, 0, k){
            ans *= a[i];
            ans %= mod;
        }
        if(ans < 0) ans += mod;
        cout << ans << endl;
        return 0;
    }

    if(n == nn && k % 2 == 1){
        ll ans = 1;
        sort(ALL(a), greater<ll>());
        rep(i, 0, k){
            // cerr << i << ": " << a[i] << endl;
            ans *= a[i];
            ans %= mod;
        }
        cerr << "flr: " << ans << endl;
        if(ans < 0) ans += mod;
        cout << ans << endl;
        return 0;
    }

    for(ll i = 0; 2*i + 1 < pn; i++){
        que.push((pos[2*i] * pos[2*i+1]) % mod);
    }
    for(ll i = 0; 2*i + 1 < nn; i++){
        que.push((neg[2*i] * neg[2*i+1]) % mod);
    }
    ll ans = 1;
    ll cnt = 0;
    while(cnt < ek && !que.empty()){
        auto q = que.top();
        ans *= q;
        ans %= mod;
        // cerr << q << endl;
        que.pop();
        cnt += 2;
    }
    ll rem = k - cnt;
    if(rem == 0){
        // cerr << "pek: " << cnt << endl;
        cout << ans << endl;
        return 0;
    }
    if(pn % 2 != 0){
        ans *= pos[pn-1];
        ans %= mod;
        rem--;
    }
    if(rem == 0){
        cout << ans << endl;
        return 0;
    }
    if(zero > 0){
        cout << 0 << endl;
        return 0;
    }
    ans *= neg[nn-1];
    ans %= mod;
    if(ans < 0) ans += mod;
    cout << ans << endl;
    return 0;
}