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;
using LL = long long;
using LD = long double;
using PII = pair<int, int>;
using PLI = pair<long long, int>;
using PLL = pair<long long, long long>;
using TI = tuple<int, int, int>;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p) {
    return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
    out << "["; bool first = true;
    for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
    return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
    out << "["; bool first = true;
    for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
    return out;
}
template <typename T>
ostream& operator <<(ostream& out, const deque<T>& a) {
    out << "["; bool first = true;
    for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
    return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
    out << "{"; bool first = true;
    for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
    return out;
}
template <typename T>
ostream& operator <<(ostream& out, const unordered_set<T>& a) {
    out << "{"; bool first = true;
    for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
    return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
    out << "{"; bool first = true;
    for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
    return out;
}
template<class T> inline void YES(T condition) { 
    if(condition) cout << "YES" << '\n';
    else cout << "NO" << '\n';
}
template<class T> inline void Yes(T condition) {
    if(condition) cout << "Yes" << '\n';
    else cout << "No" << '\n';
}
template<class T> T gcd(T a, T b) {
    while (b) {
        T tmp = b;
        b = a % b;
        a = tmp;
    }
    return a;
}
template<class T> T lcm(T a, T b){
    return a / gcd(a, b) * b;
}

#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...);
}

long long qpow(long long a, long long b, int MOD) {
    if (b == 0) return 1;
    long long res = 1;
    while (b) {
        if (b & 1) res = (res * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return res;
}

const int MOD = 1e9 + 7;
const int maxn = 2e6;
int fac[maxn + 5];
int invfac[maxn + 5];
void init() {
    fac[0] = 1;
    for (int i = 1; i <= maxn; i++) fac[i] = 1LL * fac[i - 1] * i % MOD;
    invfac[maxn] = (int)(qpow(fac[maxn], MOD - 2, MOD));
    for (int i = maxn - 1; i >= 0; i--) invfac[i] = 1LL * invfac[i + 1] * (i + 1) % MOD;
}
int C(int n, int k) {
    assert(0 <= k && n >= k);
    int tmp = (int)(1LL * invfac[k] * invfac[n - k] % MOD);
    return (int)(1LL * fac[n] * tmp % MOD);
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n, k;
    cin >> n >> k;
    vector<LL> a(n), pos, zero, neg;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        if (a[i] > 0) {
            pos.push_back(a[i]);
        } else if (a[i] == 0) {
            zero.push_back(a[i]);
        } else {
            neg.push_back(a[i]);
        }
    }
    sort(pos.begin(), pos.end());
    sort(neg.begin(), neg.end());
    reverse(neg.begin(), neg.end());
    int pcnt = pos.size(), zcnt = zero.size(), ncnt = neg.size();
    LL ALL = 1;
    for (auto& v: a) ALL = (ALL * v) % MOD;
    ALL = (ALL + MOD) % MOD;
    if (k == 1) {
        LL ans = *max_element(a.begin(), a.end()) % MOD;
        cout << (ans + MOD) % MOD << '\n';
        return 0;
    }
    if (k == pcnt + zcnt + ncnt) {
        cout << ALL << '\n';
        return 0;
    }
    if (k > pcnt + ncnt) {
        cout << 0 << '\n';
        return 0;
    } else if (pcnt == 0) {
        if (k & 1) {
            if (zcnt) { cout << 0 << '\n'; return 0; }
            LL now = 1;
            for (int i = 0; i < k; i++) now = (now * neg[i]) % MOD;
            cout << (now + MOD) % MOD << '\n';
            return 0;
        } else {
            if (k > ncnt) { cout << 0 << '\n'; return 0; }
            LL now = 1;
            for (int i = ncnt - 1; i; i--) now = (now * neg[i]) % MOD;
            cout << (now + MOD) % MOD << '\n';
            return 0;
        }
    } else {
        LL now = 1;
        int i = pos.size() - 1, j = neg.size() - 1;
        for (; k; ) {
            if (k == 1) {
                if (i >= 0) now = now * pos[i--] % MOD;
                else if (j >= 1) {
                    now = now * qpow(a[0], MOD - 2, MOD) % MOD;
                    now = now * abs(neg[j--]) % MOD;
                    now = now * abs(neg[j--]) % MOD;
                } else {
                    now = now * a[j] % MOD;
                }
                k--;
            } else {
                LL P = 0, N = 0;
                if (i >= 1) P = pos[i] * pos[i - 1];
                else if (i == 0) P = pos[i];
                if (j >= 1) N = neg[j] * neg[j - 1];
                else if (j == 0) N = neg[j];
                if (P > N) {
                    now = now * (P % MOD) % MOD;
                    k -= 1 + (i > 0);
                    i -= 1 + (i > 0);
                } else {
                    now = now * (N % MOD) % MOD;
                    k -= 1 + (j > 0);
                    j -= 1 + (j > 0);
                }
            }
        }

        cout << (now + MOD) % MOD << '\n';
    }



    return 0;
}