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 ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i)>= min((a), (b))) && ((i) <= max((a), (b))))
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
#define fi first
#define se second
#define show(x) cout << #x << " is " << x << "\n";
const ll inf = 2e18;
const ll mod = 1e9 + 7;
const ld pi = 3.141592653589793238462643383279502884;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void print(ll a[], ll n){for(ll i=0;i<n;i++){cout<<a[i]<<" ";}cout<<"\n";}
ll power(ll x, ll y, ll M = inf){
	if(y<=0) return 1;
    ll ans = 1;
    x %= M;
    while(y){
        if(y&1)
            ans = (x * ans) % M;

        x = (x * x) % M;
        y >>= 1;
    }
    return ans;
}
ll modInverse(ll n) {return power(n, mod-2, mod);}
inline ll mul(ll a, ll b){ return (a * b) % mod; }
inline ll sub(ll a, ll b){ ll c = a - b; if(c < 0) c += mod; return c; }
inline ll add(ll a, ll b){ ll c = a + b; if(c >= mod) c -= mod; return c; }
inline ll divi(ll a, ll b){ return mul(a, modInverse(b)); }

//------------------------------------------------------------------------------------------

//const ll N = 1e5 + 1;
//vector<ll> adj[N];
//bool visited[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cout<<fixed<<setprecision(10);

	ll n, k, x;
	cin>>n>>k;
	vi neg, pos;
	for(ll i=0;i<n;i++){
		cin>>x;
		if(x<0) neg.pb(x);
		else pos.pb(x);
	}
	if(pos.size()==0){
		ll ans=1;
		if(k%2==0){
			sort(all(neg));
			for(ll i=0;i<k;i++){
				ans = mul(ans, neg[i]);
			}
		}
		else{
			sort(all(neg), greater<ll>());
			for(ll i=0;i<k;i++){
				ans = mul(ans, neg[i]);
			}
		}
  		if(ans<0) ans+=2*mod;
		cout<<ans%mod;
	}
	else{
		ll ans=1;
		sort(all(neg));
		sort(all(pos), greater<ll>());
		ll i=0, j=0;
		j = min(k, (ll)(pos.size()));
		i = k-j;
		if(i%2==1 && i<(ll)neg.size()-1) {
			j--;i++;
		}
		ll op1, op2;
		while(1){
			if(i+2<=(ll)(neg.size()) && j>=2){
				op1 = neg[i]*neg[i+1];
				op2 = pos[j-1]*pos[j-2];
				if(op1>op2){
					i+=2;
					j-=2;
				}
				else break;
			}
			else break;
		}
		for(ll it=0;it<j;it++){
			ans = mul(ans, pos[it]);
		}
		for(ll it=0;it<i;it++){
			ans = mul(ans, neg[it]);
		}
		if(ans<0) ans+=2*mod;
		cout<<ans%mod;
	}
    return 0;
}