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 "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

void solve();
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}

/*================================*/
#if DEBUG
#define SIZE 10
#else
#define SIZE 223450
#endif

int N,Q,K;
int A[SIZE];

template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;

struct Item{
    int v;
    bool operator < (const Item& that) const {
        return abs(v) < abs(that.v);
    }
};

int check() {
    int ret = -(1e18/2);
    REP(i,1<<N) {
        bitset<60> b(i);
        if (b.count()==K) {
            int a = 1;
            REP(j,N)if(b[j]) {
                a *= A[i];
            }
            chmax(ret, a);
        }
    }
    return ret;
}


void solve() {
    cin>>N>>K;
    REP(i,N)cin>>A[i];
    vector<int> minus, plus;
    REP(i,N) {
        if (A[i]<0) {
            minus.push_back(A[i]);
        } else {
            plus.push_back(A[i]);
        }
    }
    sort(A,A+N);
    // 絶対値小さい順
    sort(ALL(minus));
    reverse(ALL(minus));
    sort(ALL(plus));

    int d = K-plus.size();
    if (d%2) {
        // 奇数→全部マイナスだとまずい
        if (d<(int)minus.size() && plus.size()>0) {
            // 正にできる
        } else {
            // 負になる→絶対値の小さいものを使う
            mint ans = -1;
            REP(i,N) A[i]=abs(A[i]);
            sort(A,A+N);
            REP(i,K) ans*=A[i];
            cout << ans << endl;
            return;
        }
    }
    // 絶対値の大きなものをとる
    mint ans = 1;
    while(K) {
        if (K%2 && plus.size()) {
            ans *= plus.back();
            debug("* %lld -> ans: %lld\n", plus.back(), ans);
            plus.pop_back();
            K--;
        } else if (K>=2 && minus.size() >=2) {
            auto it = minus.end();
            int mlast = (*--it);
            int mlast2 = (*--it);
            if (plus.size()<=1) {
                ans *= mlast;
                ans *= mlast2;
                minus.pop_back();
                minus.pop_back();
                K -= 2;
                continue;
            }
            auto itp = plus.end();
            int plast = (*--itp);
            int plast2 = (*--itp);
            if (mlast*mlast2 > plast*plast2) {
                ans *= mlast;
                ans *= mlast2;
                debug("%lld * %lld -> ans: %lld\n", mlast, mlast2, ans);
                minus.pop_back();
                minus.pop_back();
            } else {
                ans *= plast;
                ans *= plast2;
                debug("%lld * %lld -> ans: %lld\n", plast, plast2, ans);
                plus.pop_back();
                plus.pop_back();
            }
            K -= 2;
        } else {
            ans *= plus.back();
            debug("* %lld -> ans: %lld\n", plus.back(), ans);
            plus.pop_back();
            K--;
        }
    }
    cout << ans << endl;
}