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;

typedef long long ll;
typedef long double ld;
const ll M=1e9+7;
const ll MAXN=1e6+7;
const ld pi=acos(-1);

#define gi(n) ll n=read()
#define endl '\n'
#define rep(i,a,b) for(ll i=a; i<b; ++i)
#define per(i,a,b) for(ll i=b-1; i>=a; --i)

typedef vector<ll> V;
typedef vector<V> VV;
typedef set<ll> S;
typedef pair<ll,ll> pl;
typedef vector<pl> Vp;

#define all(v) v.begin(),v.end()
#define pb push_back
#define fs first
#define sc second

inline ll read(){
	ll x=0,ff=1;
	char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-') ff=-1;ch=getchar();}
	while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*ff;
}

ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll mul(ll a,ll b,ll P){return b?(mul(a*2%P,b>>1,P)+(b&1?a:0))%P:0;}
ll pow1(ll p,ll d, ll P){return d?mul(pow1(mul(p,p,P),d>>1,P),(d&1?p%P:1),P):1;}

V prime;
bool is_composite[MAXN];
void sieve (ll n){
	std::fill (is_composite, is_composite + n, false);
	rep(i,2,n){
		if(!is_composite[i]) prime.pb(i);
		for(ll j = 0; j < prime.size() && i*prime[j] < n; ++j){
			is_composite[i*prime[j]] = true;
			if(i % prime[j] == 0) break;
		}
	}
}
ll modInverse(ll a, ll m){
	ll m0=m,y=0,x=1;
	if(m==1) return 0;
	while(a>1){
		ll q=a/m,t=m;
		m=a%m;
		a=t; 
		t=y;

		y = x-q*y; 
		x = t;
	}
	if(x<0) x += m0;
	return x; 
}

ll f(ll a,ll b){return abs(a)<abs(b);}

void doit(){
	gi(n); gi(k);
	ll a[n],p=1,neg=0,zero=0;
	rep(i,0,n) a[i]=read();
	if(n==k){
		rep(i,0,n){
			p*=a[i];
			if(p<0) p=M-abs(p)%M;
			p%=M;
		}
		cout<<p;
		return;
	}
	sort(a,a+n,f);
	rep(i,n-k,n) neg+=a[i]<0, zero+=!a[i];
	if(zero){cout<<0; return;}
	if(neg+1&1){
		rep(i,n-k,n){
			p*=a[i];
			if(p<0) p=M-abs(p)%M;
			p%=M;
		}
		cout<<p;
		return;
	}
	if(neg==k){
		rep(i,n-k+1,n){
			p*=a[i];
			if(p<0) p=M-abs(p)%M;
			p%=M;
		}
		ll f=0;
		per(i,0,n-k+1) if(a[i]>=0){
			p*=a[i];
			p%=M;
			f=1;
			break;
		}
		if(f)cout<<p;
		else{
			p=1;
			rep(i,n-k,n-1){
				p*=a[i];
				if(p<0) p=M-abs(p)%M;
				p%=M;
			}
			p*=a[0];
			if(p<0) p=M-abs(p)%M;
			p%=M;
			cout<<p;
		}
		return;
	}
	rep(i,n-k+1,n){
		p*=a[i];
		if(p<0) p=M-abs(p)%M;
		p%=M;
	}
	if(a[n-k-1]<0){
		ll f=0;
		rep(i,n-k+1,n){
			if(a[i]<0||f){
				p*=a[i];
				if(p<0) p=M-abs(p)%M;
				p%=M;
			}
			if(a[i]>0) f=1;
		}
		p*=a[n-k-1];
		p=M-abs(p)%M;
		p%=M;
		cout<<p;
		return;
	}
	if(a[n-k-1]==0){cout<<0; return;}
	ll f=0;
	rep(i,n-k+1,n){
		if(a[i]>0||f){
			p*=a[i];
			if(p<0) p=M-abs(p)%M;
			p%=M;
		}
		if(a[i]<0) f=1;
	}
	p*=a[n-k-1];
	p=M-abs(p)%M;
	p%=M;
	cout<<p;
}
int main(){
	//for(gi(t);t--;cout<<endl)
		doit();
	return 0;
}