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: Runtime Error)

/*input
10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1
*/

#include <bits/stdc++.h>
#include <unistd.h>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define UQ(x) (x).resize(distance((x).begin(),unique(x.begin(),x.end())))

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}


void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

void solve(){
	ll i,j,_;
	ll k,m;
	cin>>m>>k;
	vll a;
	ll cz=0;
	rep(i,0,m){cin>>j;if(j)a.pb(j);else cz=1;}
	sort(a.rbegin(),a.rend(),[](ll &b,ll &c){
		return abs(c)>=abs(b);
	});
	// for(auto i: a)cout<<i<<" ";cout<<endl;
	if(a.size()<k){
		cout<<0<<endl;
		return;
	}
	vll n,p;
	rep(i,0,k){
		if(a[i]<0)n.pb(a[i]);
		else p.pb(a[i]);
	}
	if(n.size()%2==0){
		ll ans=1;
		for(auto i : p)ans*=i,ans%=M;
		for(auto i : n)ans*=(-i),ans%=M;
		cout<<ans<<endl;
		return;
	}
	ll ap=0,an=0;
	rep(i,k,a.size()){
		if(a[i]>0){
			ap=a[i];
			break;
		}
	}
	if(p.size())
	rep(i,k,a.size()){
		if(a[i]<0){
			an=a[i];
			break;
		}
	}
	if(ap==0&&an==0){
		if(cz){
			cout<<0<<endl;
			return;
		}
		ll ans=1;
		reverse(a.begin(),a.end());
		rep(i,0,k)ans*=abs(a[i]),ans%=M;
		cout<<(M-ans)%M<<endl;
		return;
	}
	if(!ap){
		ll ans=1;
		rep(i,1,p.size())ans*=p[i],ans%=M;
		for(auto i : n)ans*=abs(i),ans%=M;
		ans*=an;
		ans%=M;
		cout<<ans<<endl;
		return;
	}
	if(!an){
		ll ans=1;
		rep(i,1,n.size())ans*=abs(n[i]),ans%=M;
		for(auto i : p)ans*=abs(i),ans%=M;
		ans*=ap;
		ans%=M;
		cout<<ans<<endl;
		return;
	}
	if(ap*n[0] > an*p[0]){
		ll ans=1;
		rep(i,1,n.size())ans*=abs(n[i]),ans%=M;
		for(auto i : p)ans*=abs(i),ans%=M;
		ans*=ap;
		ans%=M;
		cout<<ans<<endl;
	}else{
		ll ans=1;
		rep(i,1,p.size())ans*=p[i],ans%=M;
		for(auto i : n)ans*=abs(i),ans%=M;
		ans*=an;
		ans%=M;
		cout<<ans<<endl;
	}
	

}

int main()
{
	ios::sync_with_stdio(0);
 	cin.tie(0);
	ll T=1;
	//cin>>T;
	while(T--){
		solve();
	}
	return 0;
}