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;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
    cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args&&... args) {
    const char *comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | ";
    __f(comma + 1, args...);
}

#define pb             push_back
#define sz(x)          (int)((x).size())
#define all(x)         (x).begin(), (x).end()
#define rep(i, n)  	   for (int i = 0; i < n; ++i)
#define irep(i, a, b)  for (int i = a; i <= b; ++i)
#define repr(i, a, b)  for (int i = a; i >= b; --i)
#define iter(it, x)    for (auto it = x.begin(); it != x.end(); ++it)

using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
const char nl = '\n';
const int inf = 2e9;
const ll INF = (ll)2e18;
const ll mod = 1e9 + 7;
const int N = 2e6 + 15;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, k;
	cin >> n >> k;
	vector<ll> a(n);
	int pos = 0, neg = 0;
	int z = 0;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		if (a[i] < 0) neg++;
		else if (a[i] > 0) pos++;
		if (a[i] == 0) z++;
	}
	ll ans = 1;
	if (pos + (neg/2)*2 >= k) {
		sort(all(a));
		int lo = 0, hi = n - 1;
		while (k > 1) {
			ll mul = 1;
			if (a[hi] * a[hi - 1] >= a[lo] * a[lo + 1]) {
				mul = (a[hi] * a[hi - 1]) % mod;
				hi -= 2;
			} else {
				mul = (a[lo] * a[lo + 1]) % mod;
				lo += 2;
			}
			ans = (ans * mul) % mod;
			k -= 2;
		}
		if (k) ans = (ans * a[hi]) % mod; 
	} else {
		for (int i = 0; i < n; ++i) {
			if (a[i] < 0) a[i] = -a[i];
		}
		sort(all(a));
		for (int i = 0; i < k; ++i) {
			ans = (ans * a[i]) % mod;
		}
		ans = (ans * (mod - 1)) % mod;
		if (z) ans = 0;
	}
	if (pos + (neg/2)*2 < k and z > 0) ans = 0;
	cout << ans << nl;
    return 0;
}