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)

#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
using base = complex<double>;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define M 1000000007
#define M2 998244353
#define ll long long
#define pll pair<long, long>
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define REPI(i, a, b) for (ll i = b - 1; i >= a; i--)
#define ff first
#define ss second
#define pb push_back
#define db pop_back
#define mp make_pair
#define mt make_tuple
#define g(a, b) get<a>(b)
#define INF (ll)1e18 + 100
#define vl vector<ll>
#define vi vector<int>
#define vll vector<pair<ll, ll>>
#define vii vector<pair<int, int>>
#define all(v) v.begin(), v.end()
#define bset(a, p) ((a) | (1ll << (p)))
#define bchk(a, p) ((a) & (1ll << (p)))
#define bxor(a, p) ((a) ^ (1ll << (p)));
#define brem(a, p) (bchk(a, p) ? (bxor(a, p)) : (a))
#define INT_SIZE 32
/*SOME BITMASK KNOWLEDGE
1)x & (x - 1):sets the last one bit of x to zero
power of two exactly when x & (x − 1) = 0.
2)x & -x:sets all the one bits to zero, except last one bit
3)x | (x - 1):inverts all the bits after the last one bit*/
#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl

#define o_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define o_setll tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
ll powM(ll x, ll y, ll m)
{
    ll ans = 1, r = 1;
    x %= m;
    while (r > 0 && r <= y)
    {
        if (r & y)
        {
            ans *= x;
            ans %= m;
        }
        r <<= 1;
        x *= x;
        x %= m;
    }
    return ans;
}
ll modI(ll a, ll m)
{
    ll m0 = m, y = 0, x = 1;
    if (m == 1)
        return 0;
    while (a > 1)
    {
        ll q = a / m;
        ll t = m;
        m = a % m;
        a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
        x += m0;
    return x;
}

ll logint(ll x, ll y)
{
    ll ans = 0;
    ll a = 1;
    for (ll i = 0; i <= x; i++)
    {
        if (x < a)
        {
            return ans;
        }
        ans++;
        a *= y;
    }
    return -1;
}
ll extended(ll a, ll b, ll &x, ll &y)
{
    if (a == 0)
    {
        x = 0;
        y = 1;
        return b;
    }
    ll x1, y1;
    ll d = extended(b % a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}
ll lcm(ll x, ll y) {
    return (x * y) / (__gcd(x, y));
}
void solve() {
    ll n, k;
    cin >> n >> k;
    vl v(n);
    vl pos, neg; ll zero = 0;
    ll ans = 1;
    REP(i, 0, n) {
        cin >> v[i];
        if (v[i] == 0) {
            zero++;
        }
        else if (v[i] > 0) {
            pos.pb(v[i]);
            // ans *= v[i];
            // ans %= M;
        }
        else {
            neg.pb(v[i]);
        }
    }
    if (pos.empty() && neg.empty()) {
        cout << 0; return;
    }
    if (k % 2 && k > pos.size()) {
        if (zero) {
            cout << 0; return;
        }
        vll keep;
        REP(i, 0, neg.size()) {
            keep.pb({abs(neg[i]), -1LL});
        }
        REP(i, 0, pos.size()) {
            keep.pb({abs(pos[i]), 1LL});
        }
        sort(all(keep));
        REP(i, 0, k) {
            ans *= (keep[i].ff * keep[i].ss) % M;
            ans %= M;
        }
        cout << (ans % M + M) % M; return;
    }
    if (pos.empty()) {
        if (k % 2) {
            if (zero) {cout << 0; return;}
        }
        sort(all(neg));
        reverse(all(neg));
        REP(i, 0, k) {
            ans *= neg[i];
            ans %= M;
        }
        cout << (ans % M + M) % M; return;
    }
    priority_queue<pair<pair<ll, ll>, ll>>pq;
    sort(all(pos));
    reverse(all(pos));
    for (ll j = 0; j < pos.size(); j += 2) {
        if (j == pos.size() - 1) {
            pq.push(mp(mp(pos[j], 1), 1)); break;
        }
        pq.push({mp(pos[j]*pos[j + 1], 2), 1});
    }
    sort(all(neg));
    for (ll j = 0; j < neg.size(); j += 2) {
        if (j == neg.size() - 1) {
            pq.push({mp(neg[j], 1), -1}); break;
        }
        pq.push({mp(neg[j]*neg[j + 1], 2), -1});
    }
    ll z = zero;
    while (zero) {
        if (zero > 1) {
            pq.push({mp(0, 2), 0});
            zero -= 2;
        }
        else {
            zero--;
            pq.push({mp(0, 1), 0});
        }
    }
    zero = 0;
    ll positive = 0, negative = 0;
    while (k > 1 && !pq.empty()) {
        pair<pair<ll, ll>, ll>p = pq.top();
        pq.pop();
        if (k > 1) {
            ans *= ((p.ff.ff) % M);
            if (p.ss == 1) {
                positive += 2;
            }
            else if (p.ss < 0) {
                negative += 2;
            }
            else {
                zero += 2;
            }
            k -= p.ff.ss;
            ans %= M;
        }
    }
    // cout << k << "\n";
    // cout << positive << "\n";
    if (k) {
        if (positive < pos.size()) {
            ans *= pos[positive];
            ans %= M;
        }
        else if (negative < neg.size()) {
            ans *= neg[neg.size() - 1];
            ans %= M;
        }
        else {
            ans = 0;
        }
    }
    cout << (ans % M + M) % M << "\n";
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // freopen("b1.in", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ll ntc;
    // cin >> ntc;
    ntc = 1;
    REP(tc, 1, ntc + 1)
    {
        // cout << "Case #" << tc << ": ";
        solve();
    }
}













// vl a, b, both;
//     ll n, m, k;
//     cin >> n >> m >> k;
//     vl v(n);
//     REP(i, 0, n) {
//         ll x;
//         cin >> x;
//         v[i] = x;
//         add_f(x, 1);
//         add(x, x);
//         ll y, z;
//         cin >> y >> z;
//         if (y) {
//             if (z) {
//                 both.pb(x);
//             }
//             else {
//                 a.pb(x);
//             }
//         }
//         else {
//             if (z) {
//                 b.pb(x);
//             }
//         }
//     }
//     sort(all(a));
//     sort(all(b));
//     sort(all(both));
//     if (a.size() + both.size() < k || b.size() + both.size() < k) {
//         cout << "-1\n";
//         return;
//     }
//     ll ans = -1;
//     ll curr = 0;
//     vl prefix_a(a.size(), 0), prefix_b(b.size(), 0);
//     REP(i, 0, min(k, (ll)a.size())) {
//         add(a[i], -a[i]);
//         add_f(a[i], -1);
//     }
//     REP(i, 0, min(k, (ll)b.size())) {
//         add(b[i], -b[i]);
//         add_f(b[i], -1);
//     }
//     REP(i, 0, a.size()) {
//         prefix_a[i] = a[i];
//         if (i > 0 ) {
//             prefix_a[i] += prefix_a[i - 1];
//         }
//     }
//     REP(i, 0, b.size()) {
//         prefix_b[i] = b[i];
//         if (i > 0 ) {
//             prefix_b[i] += prefix_b[i - 1];
//         }
//     }
//     if (a.size() >= k && b.size() >= k) {
//         ll cost = prefix_a[k - 1];
//         cost += prefix_b[k - 1];
//         ll remain = m - 2 * k;
//         ll l = 0, r = N;
//         ll keep = 0;
//         while (l <= r) {
//             ll mid = (l + r) >> 1;
//             if (sum_f(mid) >= remain) {
//                 keep = mid;
//                 r = mid - 1;
//             }
//             else {
//                 l = mid + 1;
//             }
//         }
//         cost += sum(keep - 1);
//         ll dr = sum_f(keep - 1);
//         cost += (remain - dr) * keep;
//         if (ans >= 0) {
//             ans = min(ans, cost);
//         }
//         else {
//             ans = cost;
//         }
//     }
//     REP(i, 0, min(k, (ll)both.size())) {
//         //Let take i number from both
//         curr += both[i];
//         //From other take
//         if (k - (i + 1) > a.size() || k - (i + 1) > b.size()) {
//             continue;
//         }
//         if ((i + 1) + 2 * (k - (i + 1)) > m) {
//             continue;
//         }
//         add(both[i], -both[i]);
//         add_f(both[i], -1);
//         ll remove_a = k - (i + 1);
//         ll remove_b = k - (i + 1);
//         ll cost = curr;
//         if (remove_a < a.size() && remove_a >= 0) {
//             add(a[remove_a], a[remove_a]);
//             add_f(a[remove_a], 1);
//             if (remove_a > 0) {
//                 cost += prefix_a[remove_a - 1];
//             }
//         }
//         if (remove_b < b.size() && remove_b >= 0) {
//             add(b[remove_b], b[remove_b]);
//             add_f(b[remove_b], 1);
//             if (remove_b > 0) {
//                 cost += prefix_b[remove_b - 1];
//             }
//         }
//         ll remain = m - (i + 1) - 2 * (k - (i + 1));
//         ll l = 0, r = N;
//         ll keep = 0;
//         while (l <= r) {
//             ll mid = (l + r) >> 1;
//             if (sum_f(mid) >= remain) {
//                 keep = mid;
//                 r = mid - 1;
//             }
//             else {
//                 l = mid + 1;
//             }
//         }
//         cost += sum(keep - 1);
//         ll dr = sum_f(keep - 1);
//         cost += (remain - dr) * keep;
//         if (ans >= 0) {
//             ans = min(ans, cost);
//         }
//         else {
//             ans = cost;
//         }

//     }
//     cout << ans << "\n";



// vl freq(N, 0);
// ll ftree[N];
// ll sum_f (ll k) {
//     ll s = 0;
//     while (k >= 1) {
//         s += freq[k];
//         k -= k & -k;
//     }
//     return s;
// }

// void add_f(ll k, ll x) {
//     while (k <= N) {
//         freq[k] += x;
//         k += k & -k;
//     }
// }
// ll sum (ll k) {
//     ll s = 0;
//     while (k >= 1) {
//         s += ftree[k];
//         k -= k & -k;
//     }
//     return s;
// }

// void add(ll k, ll x) {
//     while (k <= N) {
//         ftree[k] += x;
//         k += k & -k;
//     }
// }