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)

//Author: AnandRaj doubleux
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll; 

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define test() int t;cin>>t;while(t--)
#define all(v) v.begin(),v.end()
#define prinp(p) cout<<p.first<<" "<<p.second<<endl
#define prinv(V) for(auto v:V) cout<<v<<" ";cout<<endl
#define take(V,f,n) for(int in=f;in<f+n;in++) cin>>V[in]
#define what(x) cerr<<#x<<" = "<<x<<endl
#define KStest() int t,t1;cin>>t;t1=t;while(t--)
#define KScout cout<<"Case #"<<t1-t<<": "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7,MAX = 1e6+5;
const ll INF = 1e18+5;
/////////////////FastExp///////////////////
ll powN(ll a,ll p)
{
	if(p==0) return 1;
	ll z=powN(a,p/2);
	z=(z*z)%MOD;
	if(p%2) z=(z*a)%MOD;
	return z;
}
/////////////////FastExp///////////////////
///////////////////PnC/////////////////////
vl Fac(MAX),Inv(MAX);
void CalcFac()
{
	Fac[0]=Inv[0]=1;
	for(int i=1;i<MAX;i++)
	{
		Fac[i]=(Fac[i-1]*i)%MOD;
		Inv[i]=powN(Fac[i],MOD-2);
	}
}

ll NCR(int n,int r)
{
	if(r>n) return 0;
	return (((Fac[n]*Inv[r])%MOD)*Inv[n-r])%MOD;
}
///////////////////PnC/////////////////////
//////////////////Sieve////////////////////
vector<bool> is_prime(MAX, true);
vi MinDiv(MAX);
void Sieve()
{
	is_prime[0] = is_prime[1] = false;
	int i,j;
	for (i = 2; i*i <= MAX; i++) 
	{
    	if (is_prime[i]) 
    	{
    		MinDiv[i]=i;
        	for (j = i * i; j <= MAX; j += i)
            {
				is_prime[j] = false;
				MinDiv[j]=i;
    		}
    	}
	}
	for(int i=2;i<=MAX;i++) if(is_prime[i]) MinDiv[i]=i;
}
//////////////////Sieve////////////////////

int main()
{
	int n,k;
	cin>>n>>k;
	vl A(n);
	int fk=k;
	take(A,0,n);
	vl Pos,Neg;
	for(int i=0;i<n;i++)
	{
		if(A[i]>0) Pos.push_back(A[i]);
		else if(A[i]<0) Neg.push_back(A[i]);
	}

	if((int)Pos.size()+(int)Neg.size()<k)
	{
		// cout<<"Here"<<endl;
		cout<<0<<endl;
	}
	else
	{
		sort(all(Pos));
		sort(all(Neg));
		int pos=Pos.size();
		int neg=Neg.size();
		if(k==1)
		{
			ll ans=-MOD;
			for(int i=0;i<n;i++)
			{
				ans=max(ans,A[i]);
			}
			if(ans<0) ans+=MOD;
			cout<<ans<<endl;
		}
		else if(k==2)
		{
			ll ans=-MOD;

			ll temp1=-INF,temp2=-INF;
			if(pos>=2) temp1=Pos[pos-1]*Pos[pos-2];
			if(neg>=2) temp2=Neg[0]*Neg[1];

			if(temp1==-INF&&temp2==-INF)
			{
				ans = Pos[0]*Neg[0];
			}
			else
			{
				ans = max(temp1,temp2);
			}

			ans%=MOD;
			if(ans<0) ans+=MOD;
			cout<<ans<<endl;
		}
		else
		{

			ll ans=1;
			int i=0,j=pos-1;

			while(k>=2)
			{
				ll temp1=-1,temp2=-1;
				if(j-1>=0) temp1=Pos[j]*Pos[j-1];
				if(i+1<neg) temp2=Neg[i]*Neg[i+1];

				if(temp1<temp2)
				{
					i+=2;
					k-=2;
					temp2%=MOD;
					ans*=temp2;
					ans%=MOD;
				}
				else
				{
					j-=2;
					k-=2;
					temp1%=MOD;
					ans*=temp1;
					ans%=MOD;
				}
			}

			if(k==0)
			{
				cout<<ans<<endl;
			}
			else
			{
				if(j>=0) ans*=Pos[j];
				else
				{
					if(i+1<neg&&pos)
					{
						ans*=powN(Pos[0],MOD-2);
						ans%=MOD;

						ll temp = Neg[i]*Neg[i+1];
						temp%=MOD;
						ans*=temp;
						ans%=MOD;
					}
					else
					{
						ans=1;
						i=0,j=0;
						reverse(all(Neg));
						while(fk)
						{
							if(i==pos)
							{
								ans*=Neg[j];
								fk--;
								j++;
								ans%=MOD;
							}
							else if(j==neg)
							{
								ans*=Pos[i];
								fk--;
								i++;
								ans%=MOD;
							}
							else if(Pos[i]<abs(Neg[j]))
							{
								ans*=Pos[i];
								ans%=MOD;
								fk--;
								i++;
							}
							else
							{
								ans*=Neg[j];
								ans%=MOD;
								fk--;
								j++;
							}
						}
					}
				}

				ans%=MOD;
				if(ans<0) ans+=MOD;
				cout<<ans<<endl;
			}
		}
	}
}