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>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<algorithm>
#include<utility>
#include<bitset>
#include<set>
#include<map>
#define ll long long
#define db double
#define INF 10000000000000010ll
#define ldb long double
#define pb push_back
#define put_(x) printf("%d ",x);
#define get(x) x=read()
#define gt(x) scanf("%d",&x)
#define gi(x) scanf("%lf",&x)
#define put(x) printf("%d\n",x)
#define putl(x) printf("%lld\n",x)
#define gc(a) scanf("%s",a+1)
#define rep(p,n,i) for(RE ll i=p;i<=n;++i)
#define go(x) for(ll i=lin[x],tn=ver[i];i;tn=ver[i=nex[i]])
#define fep(n,p,i) for(RE ll i=n;i>=p;--i)
#define vep(p,n,i) for(RE ll i=p;i<n;++i)
#define pii pair<ll,ll>
#define mk make_pair
#define RE register
#define P 1000000007
#define gf(x) scanf("%lf",&x)
#define pf(x) ((x)*(x))
#define uint unsigned long long
#define ui unsigned
#define EPS 1e-8
#define sq sqrt
#define S second
#define F first
#define mod 1000000007
using namespace std;
char buf[1<<15],*fs,*ft;
inline char getc()
{
    return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
}
inline ll read()
{
    RE ll x=0,f=1;RE char ch=getc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getc();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getc();}
    return x*f;
}
const ll MAXN=200010;
ll n,k,cnt1,cnt2,ww;
ll a[MAXN],b[MAXN],vis[MAXN],c[MAXN];
priority_queue<pii>q;
inline ll cmp(ll x,ll y){return x>y;}
signed main()
{
	//freopen("1.in","r",stdin);
	get(n);ww=get(k);ll ans=1;ll cnt=0;
	rep(1,n,i)
	{
		ll get(x);c[i]=x;
		if(!x){++cnt;continue;}
		if(x<0)b[++cnt2]=x;
		else a[++cnt1]=x;
	}
	if(n-cnt<k){puts("0");return 0;}
	sort(a+1,a+1+cnt1,cmp);
	sort(b+1,b+1+cnt2);
	for(int i=2;i<=cnt1;i+=2)q.push(mk(a[i]*a[i-1],i));
	for(int i=2;i<=cnt2;i+=2)q.push(mk(b[i]*b[i-1],i+cnt1));
	int mark=0;if(k&1)mark=1,--k;
	while(k&&q.size())
	{
		pii w=q.top();
		vis[w.S]=1;
		ans=w.F%mod*ans%mod;
		q.pop();k-=2;
	}
	if(k){puts("0");return 0;}
	if(mark)
	{
		rep(1,cnt1,i)if(!vis[i]&&mark)ans=ans*a[i]%mod,mark=0;
		if(!mark){putl(ans);return 0;}
		if(mark&&cnt){puts("0");return 0;}
		else
		{
			rep(1,n,i)c[i]=abs(c[i]);
			sort(c+1,c+1+n);ans=1;
			rep(1,ww,i)ans=ans*c[i]%mod;
			ans=mod-ans;putl(ans);return 0;
		}
	}
	putl(ans);
	return 0;
}