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 rep(i, n) for(int i = 0; i < (n); ++i)
#define repr(i, n) for(int i = (n-1); i >= 0; --i)

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

struct mint {
    long long x;
    mint(long long x=0):x(x % MOD + (0 <= x ? 0 : 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 -= a.x) < 0) x += MOD; return *this; }
    mint& operator*=(const mint& a) { (x *= a.x) %= MOD; return *this; }
    mint operator+(const mint& a) const { return mint(x) += a; }
    mint operator-(const mint& a) const { return mint(x) -= a; }
    mint operator*(const mint& a) const { return mint(x) *= a; }

    // for prime mod
    mint inv() const {
        long long a = x, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        return mint(u);
    }
    mint& operator/=(const mint& a) { return *this *= a.inv(); }
    mint operator/(const mint& a) const { return mint(x) /= a; }
};
istream& operator>>(istream& is, const mint& a){ return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a){ return os << a.x; }

int main(){
    int N, K;
    cin >> N >> K;

    vector<int> A(N);
    for (auto &i : A) cin >> i;

    sort(A.begin(), A.end(), [](int a, int b){return abs(a) > abs(b);});

    bool minus = false;
    rep(i, K) {
        if (A[i] < 0) minus = !minus;
    }

    if (!minus) {
        mint ans = 1;
        rep(i, K) ans *= A[i];
        cout << ans << endl;
        return 0;
    }

    bool hasPlus = false;
    for (auto &i : A) {
        if (0 < i) hasPlus = true;
    }

    if (!hasPlus) {
        mint ans = 1;
        rep(i, K) ans *= A[N-1-i];
        cout << ans << endl;
        return 0;
    }

    int idx_of_p = -1, idx_of_n = -1;
    repr(i, K) {
        if (0 < A[i]) {
            idx_of_p = i;
            break;
        }
    }
    repr(i, K) {
        if (A[i] < 0) {
            idx_of_n = i;
            break;
        }
    }

    mint ans = 1;
    rep(i, K) {
        if (i == idx_of_p || i == idx_of_n) continue;
        ans *= A[i];
    }

    ll res_of_p = 0;
    if (idx_of_p != -1) {
        for (int i = K; i < N; ++i) {
            if (0 <= A[i]) {
                res_of_p = (ll)A[idx_of_p] * A[i];
                break;
            }
        }
    }

    ll res_of_n = 0;
    if (idx_of_n != -1) {
        for (int i = K; i < N; ++i) {
            if (A[i] <= 0) {
                res_of_n = (ll)A[idx_of_n] * A[i];
                break;
            }
        }
    }

    ans *= max(res_of_p, res_of_n);
    cout << ans << endl;
}