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: Accepted)

#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>
#include <deque>
#include <complex>

using namespace std;

/*
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
*/

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T& a, T b) { a = min(a, b);}
template <class T> void chmax(T& a, T b) { a = max(a, b);}

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

struct mint {
	ll x;
	mint(ll x=0):x((x%MOD+MOD)%MOD){}
	mint operator-() const { return mint(-x);}
	mint& operator+=(const mint a) {if ((x+=a.x)>=MOD) x-=MOD; return *this;}
	mint& operator-=(const mint a) {if ((x+=MOD-a.x)>=MOD) x-=MOD; return *this;}
	mint& operator*=(const mint a) {(x*=a.x)%=MOD; return *this;}
	mint operator+(const mint a) const {mint res(*this); return res+=a;}
	mint operator-(const mint a) const {mint res(*this); return res-=a;}
	mint operator*(const mint a) const {mint res(*this); return res*=a;}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t>>1);
		a *= a;
		if (t&1) a *= *this;
 		return a;
	}

	// For prime mod.
	// Do not use if MOD is not prime number !!
	mint inv() const { return pow(MOD-2);}
	mint& operator/=(const mint a) { return (*this) *= a.inv();}
	mint operator/(const mint a) const {mint res(*this); return res/=a;}
};

ll plus_ans(vll& p, vll& m, ll k) {
    if((ll)p.size() + (ll)m.size() < k) return 0;
    mint ret = 1;
    if((ll)p.size() > 0) {
        sort(p.begin(), p.end(), greater<ll>());
    }
    vll m2;
    for(auto i : m) m2.push_back(-i);
    if((ll)m2.size() > 0) {
        sort(m2.begin(), m2.end(), greater<ll>());
    }
    ll pi = 0, mi = 0;
    while(pi + mi < k) {
//        cout << pi << ':' << mi << endl;
        if(pi < (ll)p.size() - 1 && mi < (ll)m2.size() - 1 && pi + mi < k - 1) {
//            cout << (m2.size() - 1) << endl;
//            cout << "a" << endl;
            if(p[pi] * p[pi+1] >= m2[mi] * m2[mi+1]) {
                ret *= p[pi++];
            }
            else {
                ret *= m2[mi];
                ret *= m2[mi+1];
                mi += 2;
            }
        }
        else if(mi >= (ll)m2.size() - 1 || (ll)m2.size() == 0) {
//            cout << "b" << endl;
            ret *= p[pi++];
        }
        else if(k - pi - mi >= 2 && mi < (ll)m.size() - 1) { // pは残り1つ
//            cout << "c" << endl;
            ret *= m2[mi];
            ret *= m2[mi+1];
            mi += 2;
        }
        else {
            ret *= p[pi++];
        }
    }
    return ret.x;
}

ll minus_ans(vll& p, vll& m, ll k) {
    if(p.size() > 0) {
        sort(p.begin(), p.end());
    }
    sort(m.begin(), m.end(), greater<ll>());
    mint ret = 1;
    for(ll i = 0; i < min(k, (ll)m.size()); i++) {
        ret *= m[i];
    }
    for(ll i = 0; i < k - (ll)m.size() && (ll)p.size() > 0; i++) {
        ret *= p[i];
    }
//    cout << ret.x << endl;
    return ret.x;
}

bool checkp(ll p, ll m, ll k) {
    ll a = min((k/2)*2, (m/2)*2);
    return p + a >= k;
}

void solve() {
    ll n, k;
    cin >> n >> k;
    vll a(n);
    rep(i, n) cin >> a[i];
    vll pl, mi;
    for(ll i = 0; i < n; i++) {
        if(a[i] > 0) pl.push_back(a[i]);
        else if(a[i] < 0) mi.push_back(a[i]);
    }
    ll ans = 0;
    if(checkp((ll)pl.size(), (ll)mi.size(), k)) {
//        cout << "plus" << endl;
        ans = plus_ans(pl, mi, k);
    }
    else if((ll)pl.size() + (ll)mi.size() < k) ans = 0;
    else if((ll)pl.size() + (ll)mi.size() == n) {
//        cout << "minus" << endl;
        ans = minus_ans(pl, mi, k);
    }
    else ans = 0;
    cout << ans << endl;
    return;
}

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
}