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)

/*
このコード、と～おれ!
Be accepted!
∧＿∧　
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
*/

#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>

////多倍長整数, cpp_intで宣言
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;

//#pragma gcc target ("avx2")
//#pragma gcc optimization ("Ofast")
//#pragma gcc optimization ("unroll-loops")
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, n) for(int i = 1; i <= (n); ++i)
#define rep2(i, n) for(int i = 2; i < (n); ++i)
#define repr(i, n) for(int i = n; i >= 0; --i)
#define reprm(i, n) for(int i = n - 1; i >= 0; --i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a) //少数出力
#define printLdb(a) printf("%.50Lf\n", a) //少数出力
#define printdbd(a) printf("%.16lf\n", a) //少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str()) //string出力
#define all(x) (x).begin(), (x).end()
#define allsum(a, b, c) ((a + b) * c / 2LL) //等差数列の和、初項,末項,項数
#define pb push_back
#define rpriq priq<int, vector<int>, greater<int>>
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d)) /*(a, b) から (c, d) のマンハッタン距離 */
#define inf numeric_limits<double>::infinity();
#define linf numeric_limits<long double>::infinity()

using ll = long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);

using namespace std;

void scans(string & str) {
	char c;
	str = "";
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);
	while (c != '\n' && c != -1 && c != ' ') {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

ll LSB(ll n) { return (n & (-n)); }

/*-----------------------------------------ここからコード-----------------------------------------*/

/*
* @title modint
* @docs kyopro/docs/modint.md
*/

template<int mod>
struct modint {
	int val, size;

	vector<ll> fac, inv, facinv;

	modint() : val(0), size(0) {};
	modint(ll x) : val(x >= 0 ? x % mod : (mod + x % mod) % mod), size(0) {};

	//siz <= 1e7 くらい
	void cominit(const int siz) {
		size = siz;
		fac.assign(siz + 1, 0);
		inv.assign(siz + 1, 0);
		facinv.assign(siz + 1, 0);
		fac[0] = fac[1] = facinv[0] = facinv[1] = inv[0] = 1;
		for (ll i = 2; i <= siz; ++i) {
			fac[i] = fac[i - 1] * i % mod;
			inv[i] = mod - inv[mod % i] * (mod / i) % mod;
			facinv[i] = facinv[i - 1] * inv[i] % mod;
		}
	}

	modint& operator=(const modint& x) {
		val = x.val;
		return *this;
	}

	modint& operator+=(const modint& x) {
		val += x.val;
		if (val >= mod)val -= mod;
		return *this;
	}

	modint& operator-=(const modint& x) {
		val += mod - x.val;
		if (val >= mod)val -= mod;
		return *this;
	}

	modint& operator*=(const modint& x) {
		val = (int)((ll)val * (ll)x.val % mod);
		return *this;
	}

	modint& operator/=(const modint& x) {
		if (x <= size) {
			ll num = x.val;
			num *= inv[x];
			num %= mod;
			val = num;
			return *this;
		}
		int a = x.val, b = mod, u = 1, v = 0, t;
		while (b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		*this *= modint(u);
		return *this;
	}

	modint operator++() {
		val = (val + 1 == mod ? 0 : val + 1);
		return *this;
	}

	modint operator--() {
		val = (val == 0 ? mod - 1 : val - 1);
		return *this;
	}

	modint operator+(const modint& x) const {
		return (modint(*this) += x);
	}

	modint operator-(const modint& x) const {
		return (modint(*this) -= x);
	}

	modint operator*(const modint& x) const {
		return (modint(*this) *= x);
	}

	modint operator/(const modint& x) const {
		return (modint(*this) /= x);
	}

	bool operator==(const modint& x)const {
		return (val == x.val);
	}

	bool operator!=(const modint& x)const {
		return (val != x.val);
	}

	bool operator<(const modint& x)const {
		return (val < x.val);
	}

	bool operator>(const modint& x)const {
		return (val > x.val);
	}

	modint pow(ll n) {
		modint ret(1), a(val);
		while (n > 0) {
			if (n % 2) ret *= a;
			a *= a;
			n /= 2;
		}
		return ret;
	}

	modint comb(const modint& n, const modint& r) {
		return (n < r or n < 0 or r < 0) ? 0 : ((fac[n] * (facinv[r] * facinv[n - r] % mod)) % mod);
	}

	static int getmod() { return mod; };
};

int main() {

	int n, k, kk;
	scanf("%d%d", &n, &k);
	kk = k;
	vector<ll> a(n);
	rep(i, n)scanf("%lld", &a[i]);
	sort(all(a), greater<ll>());
	deque<ll> que, que2;
	rep(i, n)que.push_back(a[i]);
	vector<ll> ans, ans2;
	for (; k > 0; k -= 2) {
		if (k == 1) {
			ans.push_back(que.front());
			que.pop_front();
		}
		else {
			ll f1 = 0, f2 = 0, b1 = 1e9 + 1, b2 = 1e9 + 1;
			f1 = que.front();
			que.pop_front();
			f2 = que.front();
			que.pop_front();
			if (not que.empty()) {
				b1 = que.back();
				que.pop_back();
			}
			if (not que.empty()) {
				b2 = que.back();
				que.pop_back();
			}
			if (b1 == 1e9 + 1) {
				ans.push_back(f1 * f2);
			}
			else if (b2 == 1e9 + 1) {
				if (f1 * f2 > f2 * b1) {
					ans.push_back(f1 * f2);
					que.push_back(b1);
				}
				else {
					ans.push_back(b1 * f2);
					que.push_front(f1);
				}
			}
			else {
				if (f1 * f2 > b1 * b2) {
					ans.push_back(f1 * f2);
					que.push_back(b2);
					que.push_back(b1);
				}
				else {
					ans.push_back(b1 * b2);
					que.push_front(f2);
					que.push_front(f1);
				}
			}
		}
	}

	vector<int> p, q;
	rep(i, n) {
		if (a[i] < 0)q.push_back(a[i]);
		else p.push_back(a[i]);
	}
	sort(all(p), greater<int>());
	sort(all(q));
	for (const auto& aa : p)que2.push_front(aa);
	for (const auto& aa : q)que2.push_back(aa);
	que = que2;
	k = kk;

	for (; k > 0; k -= 2) {
		if (k == 1) {
			ans2.push_back(que.back());
			que.pop_back();
		}
		else {
			ll f1 = 1e9+1, f2 = 1e9+1, b1 = 1e9 + 1, b2 = 1e9 + 1;
			b1 = que.back();
			que.pop_back();
			b2 = que.back();
			que.pop_back();
			if (not que.empty()) {
				f1 = que.front();
				que.pop_front();
			}
			if (not que.empty()) {
				f2 = que.front();
				que.pop_front();
			}
			if (f1 == 1e9 + 1) {
				ans2.push_back(b1 * b2);
			}
			else if (f2 == 1e9 + 1) {
				if (b1 * b2 < b2 * f1) {
					ans2.push_back(b1 * b2);
					que.push_back(f1);
				}
				else {
					ans2.push_back(b2 * f1);
					que.push_front(b1);
				}
			}
			else {
				if (f1 * f2 < b1 * b2) {
					ans2.push_back(f1 * f2);
					que.push_back(b2);
					que.push_back(b1);
				}
				else {
					ans2.push_back(b1 * b2);
					que.push_front(f2);
					que.push_front(f1);
				}
			}
		}
	}
	ll x = 1, y = 1, xcnt = 0, ycnt = 0;
	bool xminus = false, yminus = false;
	for (auto& aa : ans) {
		aa %= MOD;
		if (aa < 0)aa += MOD, xminus ^= 1;
		x *= (aa % MOD);
		xcnt += x / (ll)(MOD);
		x %= MOD;
	}
	for (auto& aa : ans2) {
		aa %= MOD;
		if (aa < 0)aa += MOD, yminus ^= 1;
		y *= (aa % MOD);
		ycnt += y / (ll)(MOD);
		y %= MOD;
	}
	if (xminus and not yminus)printf("%lld\n", y);
	else if (not xminus and yminus)printf("%lld\n", x);
	else printf("%lld\n", max(x, y));

	Please AC;
}