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>
typedef long long ll;
typedef long double ld;
using namespace std;

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back

ll gcd(ll a,ll b){return b ? gcd(b,a%b) : a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
 
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};


const ll mod = 1e9 + 7;

long long modinv(long long a) {
    ll m = mod;
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u % m;
}

int main(){
    ll n, k; cin >> n >> k;
    vector<vector<ll>> alis(n, vector<ll>(2, 0));
    ll cc = 0;
    REP(i, 0, n){
        cin >> alis[i][0];
        alis[i][1] = abs(alis[i][0]);
        cc += (alis[i][0] < 0);
    }
    
    sort(ALL(alis), [](auto &x, auto &y){return x[1] > y[1];});
    if(cc == n && cc %2 == 1){
        ll ans = 1;
        REP(i, 0, k){
            ans *= alis[i][1];
            ans %= mod;
        }
        ans *= -1;
        cout << (ans + mod )%mod << endl;
        return 0;
    }
    ll ans = 1, count = 0;
    REP(i, 0, k){
        ans *= alis[i][1];
        ans %= mod;
        if(alis[i][0] < 0){
            count++;
        }
    }
    if(count % 2 == 0){
        cout << ans << endl;
        return 0;
    }else{
        ll fans = 0;
        ll mid1 = ans, mid2 = ans;
        int f1 = 0, f2 = 0;
        ll sub1 = -1, sub2 = -1, key1, key2;
        for(int i = k-1; i >= 0; i--){// + to -
            if(alis[i][0] > 0){
                REP(l, k, n){
                    if(alis[l][0] < 0){
                        sub1 = alis[l][1];
                    }
                    break;
                }
                if(sub1 == -1){
                    break;
                }
                mid1 *= modinv(alis[i][1]);
                key1 = alis[i][0];
                mid1 %= mod;
                mid1 *= sub1;
                mid1 %= mod;
                f1 = 1;
                break;
            }
        }
        if(f1 == 1){
            fans = mid1;
        }
        for(int i = k-1; i >= 0; i--){// - to +
            if(alis[i][0] < 0){ 
                REP(l, k, n){
                    if(alis[l][0] > 0){
                        sub2 = alis[l][1];
                    }
                    break;
                }
                if(sub2 == -1){
                    break;
                }
                mid2 *= modinv(alis[i][1]);
                key2 = alis[i][0];
                mid2 %= mod;
                mid2 *= sub2;
                mid2 %= mod;
                f2 = 1;
                break;
            }
        }
        if(f2 == 1){
            if(f1 == 0){
                fans = mid2;
            }else{
                if(abs(key1 * sub2) <= abs(key2  * sub1)){
                    fans = mid2;
                }
            }
        }
        cout << fans % mod << endl;
    }
}