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){
	return abs(x)>abs(y);
}
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lli n,k;
    cin>>n>>k;
    lli x;
    vector <lli> arr;
    lli neg=0;
    rep(i,n){
    	cin>>x;
    	arr.pb(x);
    	if(x<0)
    	neg++;
	}
	sort(all(arr),comp);
	lli mod=1000000007;
	lli ans=1;
	if(k==n){
		rep(i,n){
			ans=((ans*arr[i])%mod+mod)%mod;
		}
	}
	else if(k%2!=0 && neg==n){
		for(lli i=n-1;i>=n-k;i--){
			ans=((ans*arr[i])%mod+mod)%mod;
		}
	}
	else{
		neg=0;
		rep(i,k){
			if(arr[i]<0)
			neg++;
			ans=((ans*arr[i])%mod+mod)%mod;
		}
		if(neg%2!=0){
			lli minneg=inf,minpos=inf;
			rep(i,k){
				if(arr[i]<0){
					minneg=min(minneg,-arr[i]);
				}
				if(arr[i]>=0){
					minpos=min(minpos,arr[i]);
				}
			}
			lli maxpos=-1,maxneg=-1;
			for(lli i=k;i<n;i++){
				if(arr[i]>=0)
				maxpos=max(maxpos,arr[i]);
				if(arr[i]<0)
				maxneg=max(maxneg,-arr[i]);
			}
			//cout<<minpos<<" "<<maxpos<<" "<<minneg<<" "<<maxneg<<"\n";
			if(maxpos>=0 && maxneg>0 && minpos<inf && minneg<inf){
				if((maxpos*minpos)<(maxneg*minneg)){
				ans=((ans*(-maxneg))%mod+mod)%mod;
				ans=modDivide(ans,minpos,mod);
				}
				else{
				ans=(ans*maxpos)%mod;
				ans=modDivide(ans,-minneg,mod);
				}
			}
			else if(maxpos==-1 && maxneg>0 && minpos<inf && minneg<inf){
				ans=((ans*(-maxneg))%mod+mod)%mod;
				ans=modDivide(ans,minpos,mod);
			}
			else if(maxneg==-1 && maxpos>=0 && minpos<inf && minneg<inf){
				ans=(ans*maxpos)%mod;
				ans=modDivide(ans,-minneg,mod);
			}
			else if(minpos==inf){
				ans=(ans*maxpos)%mod;
				ans=modDivide(ans,-minneg,mod);
			}
		}
	}
	cout<<ans;
    return 0;
}