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)

#pragma target("avx")
#pragma optimize("O3")
#include<bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll,ll>;
using PP = std::pair<ll,P>;
using V = std::vector<ll>;
template<typename T> using pq = std::priority_queue<T>;
template<typename T> using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;

#define REP(i,b,e) for(ll i=b; i<e; i++)
#define ALL(vec) vec.begin(),vec.end()
#define PRINT(vec) printf("[ ");for(auto &i:vec)printf("%ld ",i);puts("]");
#define SCAN(vec) for(auto &i:vec)scanf("%ld",&i)
#define fi first
#define se second

const int MOD = 1e9+7;
const ll INF = 1e18;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

ll inv(ll x){
	ll ret = 1, p = MOD;
	while(p){
		if(p&1) ret = ret * x % MOD;
		x = x * x % MOD;
		p >>= 1;
	}
	return ret;
}

int main(){
	int n, k;
	scanf("%d %d", &n, &k);
	V a(n);
	SCAN(a);

	V pl, mi;
	REP(i, 0, n){
		if(a[i]>=0) pl.push_back(a[i]);
		else mi.push_back(a[i]);
	}
	std::sort(ALL(pl));
	std::sort(ALL(mi), std::greater<ll>());

	if(pl.size()==0){
		ll ans = 1;
		REP(i, 0, k) ans = ans * mi[i] % MOD;
		printf("%ld\n", (ans+MOD)%MOD);
		return 0;
	}

	ll ans = 1, plumin = *pl.rbegin();
	while(k){
		ll x=pl.size(), y=mi.size();
		if(k>=2){
			ll pl2 = pl[x-2] * pl[x-1], mi2 = -1;
			if(y>=2) mi2 = mi[y-2] * mi[y-1];
			
			if(x>=2 and y<2){
				ans = ans * pl[x-1] % MOD;
				pl.pop_back();
				k -= 1;
				continue;
			}else if(x<2 and y>=2){
				ans *= mi[y-2] * mi[y-1] % MOD;
				ans %= MOD;
				mi.pop_back();
				mi.pop_back();
				k -= 2;
				continue;
			}

			if(pl2>mi2){
				ans = ans * pl[x-1] % MOD;
				ans %= MOD;
				pl.pop_back();
				k -= 1;
			}else{
				ans *= mi[y-2] * mi[y-1] % MOD;
				ans %= MOD;
				mi.pop_back();
				mi.pop_back();
				k -= 2;
			}
		}else{
			if(x>0){
				ans = ans * pl[x-1] % MOD;
				k --;
			}else{
				ans = ans * mi[y-1] % MOD;
				k--;
			}
		}
	}
	printf("%ld\n", (ans+MOD)%MOD);
	return 0;
}