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 <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <memory>
#include <complex>
#include <numeric>
#include <cstdio>
#include <iomanip>
#include <random>

#define REP(i,m,n) for(int i=int(m);i<int(n);i++)
#define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i)
#define EACH(i,c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
//#define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x)  cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) do{}while(0)
#define dump(x) do{}while(0)
#define BR 
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int,int>;
using VP = vector<PII>;
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; }

constexpr int MOD = 1'000'000'007;

template<class T> inline T sqr(T x) {return x*x;}

long long power(long long x, long long n, long long mod = 1000000007) {
    if (n == 0) return 1;
    if (n % 2 == 0) return power(x * x % mod, n/2, mod);
    else return x * power(x, n-1, mod) % mod;
}

// auto mod int
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(const LL &t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime MOD
    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 operator()() const {
        return x;
    }
};
using VM = vector<mint>;
using VVM = vector<VM>;

void solve() {
    LL n,k;
    cin >> n >> k;
    VI a(n);
    REP(i,0,n) cin >> a[i];
    int z = 0;
    int pl = 0, mi = 0;
    REP(i,0,n) {
        z += a[i] == 0;
        pl += a[i] > 0;
        mi += a[i] < 0;
    }
    if (z + k > n) {
        cout << 0 << endl;
        return;
    }
    if (k == pl + mi) {
        if (mi % 2 && z > 0) {
            cout << 0 << endl;
            return;
        }
        mint ans = 1;
        REP(i,0,n) if (a[i] != 0) ans *= a[i];
        cout << ans() << endl;
        return;
    }
    if (pl == 0 && z > 0) {
        cout << 0 << endl;
        return;
    }
    if (pl == 0) {
        sort(all(a));
        if (k % 2) reverse(all(a));
        mint ans = 1;
        int cnt = 0;
        REP(i,0,n) {
            if (a[i] >= 0) continue;
            ans *= a[i];
            ++cnt;
            if (cnt == k) break;
        }
        cout << ans() << endl;
        return;
    }

    VLL ps, ms;
    REP(i,0,n) {
        if (a[i] > 0) ps.push_back(a[i]);
        if (a[i] < 0) ms.push_back(a[i]);
    }
    sort(all(ps));
    sort(all(ms));
    reverse(all(ps));
    VLL cs;
    int ip = 0, im = 0;
    mint ans = 1;
    if (k % 2) {
        ans *= ps[0];
        ++ip;
    }
    REP(i,0,k/2) {
        if (ip + 2 > ps.size()) {
            ans *= ms[im] * ms[im+1];
            im += 2;
            continue;
        }
        if (im + 2 > ms.size()) {
            ans *= ps[ip] * ps[ip+1];
            ip += 2;
            continue;
        }
        LL p = ps[ip] * ps[ip+1];
        LL m = ms[im] * ms[im+1];
        if (p >= m) {
            ans *= p;
            ip += 2;
        } else {
            ans *= m;
            im += 2;
        }
    }
    cout << ans() << endl;
}

signed main() {
    solve();
    
    return 0;
}