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 P = pair<int,int>;
using vl = vector<ll>; using vvl = vector<vl>;
#define mp make_pair
#define pb push_back
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define rrep(i,n) for (int i=(n-1);i >= 0;i--)
#define all(v) v.begin(),v.end()
#define sz(v) (int)((v).size())
#define MAX(v) *max_element(all(v))
#define MIN(v) *min_element(all(v))
#define Sort(v) sort(all(v))
#define Rev(v) reverse(all(v))
#define vec(type,name,...) vector<type> name(__VA_ARGS__)
#define VEC(type,name,size) vector<type> name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cout << H << " ";
    debug_out(T...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...) 
#endif
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
    for (std::size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
    return os;
}
inline void IN(void){return;}
template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){cin >> first;IN(rest...);return;}
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;}
const ll INF = 1LL << 60;
const ll mod = 1000000007;
const int dx[8] = {1,0,-1,0,1,1,-1,-1}; const int dy[8] = {0,1,0,-1,1,1,-1,-1};

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    ll n,k;
    cin >> n >> k;
    vector<pair<ll,ll>> data(n);
    rep(i,n){
        cin >> data[i].second;
        data[i].first = abs(data[i].second);
    }
    Sort(data);
    vector<ll> a(n);
    bool allm = true;
    rep(i,n){
        a[i] = data[i].second;
        if(a[i]>=0) allm=false;
    }
    ll ans = 1;
    if(allm && k%2==1){
        rep(i,k) ans = (ans*a[i]+mod)%mod;
        cout << ans << endl;
        return 0;
    }
    Rev(a);
    if(n==k){
        rep(i,k) ans = (ans*a[i])%mod;
        cout << ans << endl;
        return 0;
    }

    ll plus = 0, minus = 0;
    ll pindex = 0, mindex = 0;
    rep(i,k) {
        ans = (ans*a[i])%mod;
        if(a[k-i-1]>0 && plus == 0){
            plus = a[k-i-1];
            pindex = k-i-1;
        }
        if(a[k-i-1]<0 && minus == 0){
            minus = a[k-i-1];
            mindex = k-i-1;
        }
    }
    if(ans>=0){
        cout << ans << endl;
        return 0;
    }
    ans = 1;
    ll chplus = -1, chminus = 0;
    rep(i,k,n){
        if(a[i] >= 0 && chplus == -1) chplus = a[i];
        else if(a[i] < 0 && chminus == 0) chminus = a[i];
    }
    if(chplus == -1 || abs(chplus*minus) < abs(chminus*plus)){
        rep(i,k){
            if(i==pindex) continue;
            ans = (ans*a[i])%mod;
        }
        ans = (ans*chminus+mod)%mod;
    }else{
        rep(i,k){
            if(i==mindex) continue;
            ans = (ans*a[i])%mod;
        }
        ans = (ans*chplus+mod)%mod;
    }
    cout << ans << endl;
    return 0;
}