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 signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,K;
pair<int,int> A[202020];
const ll mo=1000000007;

ll modpow(ll a, ll n = mo-2) {
	ll r=1;a%=mo;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	int p=0;
	FOR(i,N) {
		cin>>x;
		A[i]={abs(x),x};
		if(x>0) p=1;
	}
	sort(A,A+N);
	
	ll ret=1;
	if(p==0 || K==N) {
		if(K%2==0) {
			FOR(i,K) (ret*=(mo+A[N-1-i].second))%=mo;
		}
		else {
			FOR(i,K) (ret*=(mo+A[i].second))%=mo;
		}
	}
	else {
		int pm=mo,mm=0;
		int s=0;
		FOR(i,K) {
			(ret*=(mo+A[N-1-i].second))%=mo;
			if(A[N-1-i].second>0) pm=A[N-1-i].second;
			if(A[N-1-i].second<0) mm=A[N-1-i].second, s^=1;
		}
		
		if(ret && s==1) {
			int p2=-1,m2=1,z=0;
			for(i=N-K;i>=0;i--) {
				if(A[i].second>0&&p2==-1) p2=A[i].second;
				if(A[i].second<0&&m2==1) m2=A[i].second;
				if(A[i].second==0) z++;
			}
			
			if((mm==0 || p2==-1) && (pm==mo&&m2==1)) {
				if(z) ret=0;
			}
			else if(mm==0 || p2==-1) {
				(ret*=(m2+mo))%=mo;
				(ret*=modpow(pm))%=mo;
			}
			else if(pm==mo&&m2==1) {
				(ret*=(p2))%=mo;
				(ret*=modpow(mm+mo))%=mo;
			}
			else if(1LL*p2*pm>=1LL*m2*mm) {
				(ret*=(p2))%=mo;
				(ret*=modpow(mm+mo))%=mo;
			}
			else {
				(ret*=(m2+mo))%=mo;
				(ret*=modpow(pm))%=mo;
			}
		}
	}
	cout<<ret<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}