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;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int , int> i_i;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> vvl;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvec;
typedef vector<vector<bool>> vveb;
typedef vector<vector<string>> vves;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<(int)(n);i++)
#define rep2(i,n) for(int i=2;i<(int)(n);i++)
#define repk(i,k,n) for(int i=k;i<(int)(n);i++)
#define rev(v) reverse(v.begin(), v.end())
#define fs first
#define sc second
#define pb push_back
#define pp pop_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
//const int64 INF = 1LL << 58;
#define dame { puts("-1"); return 0;}
#define YES { cout << "YES" << endl; return 0; }
#define NO { cout << "NO" << endl; return 0; }
#define Yes { cout << "Yes" << endl; return 0; }
#define No { cout << "No" << endl; return 0; }
#define OK { cout << "OK" << endl; return 0; }
#define NG { cout << "NG" << endl; return 0; }
#define sz(v) ((int)(v).size())
#define uniq(a) sort(all(a));(a).resize(unique(all(a)) - a.begin());
#define dab(v) for(auto i:v)cout<<i<<' '; cout << endl;
#define vin(v) for (ll i = 0; i < v.size(); ++i) cin >> v[i];
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void out(T a){cout << a << endl;}
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

#define SIZE 200005
#define MOD 1000000007
 
typedef pair <int,int> P;
 
ll inv[SIZE], fac[SIZE], finv[SIZE];
void make(){
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for(int i=2; i<SIZE; i++)
	{
		inv[i] = MOD - inv[MOD%i]*(MOD/i)%MOD;
		fac[i] = fac[i-1] * (ll) i % MOD;
		finv[i] = finv[i-1] * inv[i] % MOD;
	}
    return ;
}
ll comb(int a,int b){
	if(a < b) return 0;
	return fac[a] * (finv[b] * finv[a-b] % MOD) % MOD;
}



ll beki(int a, int n){
    if(n == 0) return 1;
    ll ans = beki(a, n / 2);
    ans = ans * ans;
    if(n % 2 == 1) ans = ans * a;
    return ans;
}


int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
    //cout << fixed << setprecision(15);
	int N,K;cin>>N>>K;
    vl A(N); vin(A);
    sort(all(A));
    int start = 0, end = N - 1;
    ll now = 1;
    if(K == 1) {
        cout << A[N-1] << endl;
        return 0;
    }

    int plus = 0, nega = 0, zero = 0;
    rep(i,N) {
        if(A[i] > 0) plus++;
        else if(A[i] < 0) nega++;
        else zero++;
    }
    if(N == 1) {cout << A[N-1] << endl; return 0;}
    if(N == K) {
        ll tmp = 1;
        rep(i,N) { tmp *= A[i]; tmp %= mod; }
        if(tmp < 0) tmp += mod;
        cout << tmp << endl;
        return 0;
    }
    if(plus == 0){
        if(K <= nega and K%2 == 1){
            vl B(N); rep(i,N) B[i] = abs(A[i]);
            sort(all(B));
            ll tmp = -1;
            rep(i,K) { tmp *= B[i]; tmp %= mod; }
            if(tmp < 0) tmp += mod;
            cout << tmp << endl;
            return 0;
        }
        if(K%2 == 0 and K <= nega){
            ll tmp = 1;
            rep(i,K){
                tmp *= abs(A[i]);
                tmp %= mod;
                
            }
            if(tmp < 0) tmp +=mod;
            cout << tmp << endl;
            return 0;
        }
        else {cout << 0 << endl; return 0; }
    }
    /*if(K - plus > 0 and (K - plus) %2 == 1) {
        if(zero != 0) {cout << 0 << endl; return 0;}
        else {
            vl B(N); rep(i,N) B[i] = abs(A[i]);
            sort(all(B));
            ll tmp = -1;
            rep(i,K) { tmp *= B[i]; tmp %= mod; }
            if(tmp < 0) tmp += mod;
            cout << tmp << endl;
            return 0;
        }
    }*/

   
    while(true){
        if(K== 0) break;
        else if(K == 1) { now *= max(A[start], A[end]) ;now%= mod; K--; }
        else {
            if(max(A[start] * A[start+1] , A[end-1] * A[end]) == 0){
                if(A[start] > 0 and A[end] > 0) {
                    now *= ((A[start] * A[end]) % mod);
                    now %= mod;
                    K -= 2;
                    start += 1;
                    end -= 1;
                }
                else{
                    cout << 0 << endl;
                    return 0;
                }
            }
            if(A[start] * A[start+1] <= A[end-1] * A[end]) {
                now *= ( (A[end-1] * A[end]) % mod);
                now %= mod;
                K -= 2;
                end -= 2;
            }
            else {
                now *= ( (A[start] * A[start+1]) % mod);
                now %= mod;
                K -= 2;
                start += 2;
            }
        }
    }
    now %= mod;
    if(now < 0) now += mod;
    out(now);
}