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 lli long long int
#define rep(i,n) for(lli i=0;i<n;i++)
#define repb(i,n) for(lli i=n-1;i>=0;i--)
#define pb push_back
#define mp make_pair
#define bg() begin()
#define en() end()
#define ft first
#define sc second
#define vect_lli_it vector<lli>::iterator
#define set_lli_it set<lli>::iterator
#define inf 1e18
#define all(X) X.begin(),X.end()
#define iterate(X) for(it=X.begin();it!=X.end();it++)
#define pii pair<lli,lli>
#define vi vector <lli>
lli combi(lli n,lli k)
{
    lli ans=1;
    k=k>n-k?n-k:k;
    lli j=1;
    for(;j<=k;j++,n--)
    {
        if(n%j==0)
        {
            ans*=n/j;
        }else
        if(ans%j==0)
        {
            ans=ans/j*n;
        }else
        {
            ans=(ans*n)/j;
        }
    }
    return ans;
}
lli gcd(lli a, lli b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
lli findlcm(vector <lli> arr, lli n)
{
    lli ans = arr[0];
    for (lli i = 1; i < n; i++)
        ans = (((arr[i] * ans)) /
                (gcd(arr[i], ans)));

    return ans;
}
lli power(lli x, lli y, lli p)
{
    lli res = 1;
    x = x % p;
    if (x == 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
lli gcdExtended(lli a, lli b, lli *x, lli *y);
lli modInverse(lli b, lli m)
{
    lli x, y;
    lli g = gcdExtended(b, m, &x, &y);
    if (g != 1)
        return -1;
    return (x%m + m) % m;
}
lli modDivide(lli a, lli b, lli m)
{
    a = a % m;
    lli inv = modInverse(b, m);
    return ((inv * a) % m+m)%m;
}
lli gcdExtended(lli a, lli b, lli *x, lli *y)
{
    if (a == 0)
    {
        *x = 0, *y = 1;
        return b;
    }
    lli x1, y1;
    lli gcd = gcdExtended(b%a, a, &x1, &y1);
    *x = y1 - (b/a) * x1;
    *y = x1;
    return gcd;
}
bool comp(lli x,lli y){
	if(x<0)
	x=-x;
	if(y<0)
	y=-y;
	return (x>y);
}
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lli n,k;
    cin>>n>>k;
    lli x;
    vector <lli> arr;
    vector <lli> pos,neg;
    rep(i,n){
    	cin>>x;
    	arr.pb(x);
    	if(x>=0)
    	pos.pb(x);
    	else
    	neg.pb(-x);
	}
	lli mod=1000000007;
	sort(all(pos),greater<lli>());
	sort(all(neg),greater<lli>());
	lli cnt=0;
	lli pro=1;
	rep(i,min((lli)pos.size(),k)){
		cnt++;
		pro=(pro*pos[i])%mod;
	}
	lli lastneg=0,lastpos=cnt-1;
	if(cnt<k){
		lli diff=k-cnt;
		if(diff%2==0){
			rep(i,diff)
			pro=((pro*(-neg[i]))%mod+mod)%mod;
			lastneg=diff;
		}
		else{
			if(cnt-1>=0)
			pro=modDivide(pro,pos[cnt-1],mod);
			else{
			diff--;
			sort(all(neg));
			}
			rep(i,diff+1)
			pro=((pro*(-neg[i]))%mod+mod)%mod;
			lastneg=diff+1;
			lastpos=cnt-1;
		}
	}
	while(lastneg+1<(lli)neg.size() && lastpos-1>=0){
		lli valneg=neg[lastneg]*neg[lastneg+1];
		lli valpos=pos[lastpos]*pos[lastpos-1];
		if(valpos<valneg){
			pro=modDivide(pro,valpos,mod);
			pro=(pro*valneg)%mod;
		}
		lastneg+=2;
		lastpos-=2;
	}
	cout<<pro;
	return 0;
}