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 pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define ll long long
#define ld long double
#define endl "\n"

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
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...);
}
#else
#define debug(...)
#endif


// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)



//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT


ll mod(ll a, ll m) {
    ll r = a % m;
    if(r < 0)
        r += m;

    return r;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n, k;
    cin >> n >> k;

    vector<ll> a(n);
    bool neg = true;
    for(int i = 0; i < n; i++) {
        cin >> a[i];
        if(a[i] >= 0)
            neg = false;
    }

    if(k == n) {
        ll prod = 1;
        for(int i = 0; i < n; i++) {
            prod = (prod*a[i])%MOD;
        }
        cout << mod(prod, MOD) << endl;
    }
    else if(k%2!=0 && neg) {
        sort(all(a), greater<ll>());
        ll prod = 1;
        for(int i = 0; i < k; i++) {
            prod = (prod*a[i])%MOD;
        }

        cout << mod(prod, MOD) << endl;
    }
    else {
        sort(all(a), [](const ll a, const ll b) {return abs(a) > abs(b); });
        ll prod = 1;
        for(int i = 0; i < k; i++) {
            prod = (prod*a[i])%MOD;
        }
        if(prod >= 0) {
            cout << mod(prod, MOD) << endl;
        }
        else {
            //remove positive, add negative
            ll remove1 = INT_MAX, remove2 = INT_MAX, add1 = INT_MAX, add2 = INT_MAX;
            for(int i = k-1; i >= 0; i--) {
                if(a[i] > 0) {
                    remove1 = a[i];
                    break;
                }
            }
            for(int i = k; i < n; i++) {
                if(a[i] < 0) {
                    add1 = a[i];
                    break;
                }
            }

            //remove negative, add non-negative
            for(int i = k-1; i >= 0; i--) {
                if(a[i] < 0) {
                    remove2 = a[i];
                    break;
                }
            }
            for(int i = k; i < n; i++) {
                if(a[i] >= 0) {
                    add2 = a[i];
                    break;
                }
            }

            double op1 = INT_MIN, op2 = INT_MIN;
            if(remove2 != INT_MAX && add2 != INT_MAX) {
                op2 = abs((double)add2 / remove2);
            }


            if(remove1 != INT_MAX && add1 != INT_MAX) {
                op1 = abs((double)add1 / remove1);
            }

            if(op1 > op2) {
                prod = (prod/remove1*add1)%MOD;
            }
            else {
                prod = (prod/remove2*add2)%MOD;
            }

            cout << mod(prod, MOD) << endl;

        }
    }

    return 0;
}