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<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<functional>
#include<utility>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define re register
#define FOR(i,a,b) for(re int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(re int i=(a);i>=(b);i--)
#define temT template<typename T>
using namespace std;
typedef long long ll;
const int N=int(2e5)+10;
const ll mo=ll(1e9)+7;
const int inf=int(2e9)+7;

temT inline T add(T x,T y){return (x+y)%mo;}
temT inline T dec(T x,T y){return (x-y+mo)%mo;}
temT inline T mul(T x,T y){return (ll)x*y%mo;}
temT inline int mpw(int x,T y){
	int res=1;
	for(;y;y>>=1,x=mul(x,x))
		if(y&1)	res=mul(res,x);
	return res;
}

int a[N],n,K,pi,pj,ct,ze;	int res=1;

inline void fix(){
	int m=0;	static int tmp[N];
	FOR(i,1,n)	if(a[i]!=0)	tmp[++m]=a[i];
	FOR(i,1,m)	a[i]=tmp[i];	n=m;
}
inline bool allmi(){FOR(i,1,n) if(a[i]>0) return 0;	return 1;}
inline bool cmp(int a1,int b1,int a2,int b2){//compare a1/b1,a2/b2
	return (ll)a1*b2>(ll)a2*b1;
}

int main(){
	cin>>n>>K;	FOR(i,1,n)	cin>>a[i];
	FOR(i,1,n)	if(a[i]==0) ze++;
	if(n-ze<K)	return cout<<0,0;
	fix();	sort(a+1,a+n+1);
	if(allmi()){
		if((K&1)==0){
			FOR(i,1,K)	res=mul(res,abs(a[i]));
			cout<<res;
		}else{
			ROF(i,n,n-K+1)	res=mul(res,abs(a[i]));
			cout<<(ze?0:mo-res);
		}return 0;
	}
	for(pi=1,pj=n,ct=0;K--;){
		if(abs(a[pi])>abs(a[pj]))
			res=mul(res,abs(a[pi])),ct+=a[pi]<0,pi++;
		else
			res=mul(res,abs(a[pj])),ct+=a[pj]<0,pj--;
	}
	if(ct&1){
		int a1=-inf,b1=-inf;//a1:min <0 , b1:max >0
		int a2=inf,b2=inf;//a2:min >0 , b2:max <0
		FOR(i,1,pi-1) if(a[i]<0) a1=max(a1,a[i]);
		FOR(i,pi,pj)	if(a[i]>0)	b1=max(b1,a[i]);
		FOR(i,pj+1,n) if(a[i]>0) a2=min(a2,a[i]);
		FOR(i,pi,pj)	if(a[i]<0)	b2=min(b2,a[i]);
		a1=abs(a1),b1=abs(b1),a2=abs(a2),b2=abs(b2);
		if(a1==inf || b1==inf)
			res=mul(res,mul(b2,mpw(a2,mo-2)));
		else if(a2==inf || b2==inf)
			res=mul(res,mul(b1,mpw(a1,mo-2)));
		else{
			if(cmp(b1,a1,b2,a2))
				res=mul(res,mul(b1,mpw(a1,mo-2)));
			else
				res=mul(res,mul(b2,mpw(a2,mo-2)));
		}
	}
	cout<<res;
	return 0;
}