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 <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
const int mod = 1e9 + 7;
//const int mod = 998244353;

ll modpow(ll x, ll n, ll mod){
    ll res = 1;
    while(n > 0){
        if(n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}


int main(){
    ll n,k; cin >> n >> k;
    vpl b(n);
    vl a(n); rep(i,n) cin >> a[i];
    rep(i,n){
        b[i].first = abs(a[i]);
        if(a[i] == 0) b[i].second = 0;
        else if(a[i] < 0) b[i].second = -1;
        else b[i].second = 1;
    }
    sort(rall(b));
    if(b[k-1].first == 0){
        puts("0");
        return 0;
    }
    ll ans = 1;
    vl minus;
    vl plus;
    rep(i,k){
        ans *= b[i].first;
        ans %= mod;
        if(b[i].second < 0) minus.push_back(b[i].first);
        else if(b[i].second > 0) plus.push_back(b[i].first);
    }
    if(minus.size() % 2 == 0){
        cout << ans << "\n";
        return 0;
    }
    sort(all(minus)); sort(all(plus));
    ll mi = 0, pl = 0;
    for(int i=k; i<n; i++){
        if(b[i].second < 0){
            mi = b[i].first;
            break;
        }
    }
    for(int i=k; i<n; i++){
        if(b[i].second > 0){
            pl = b[i].first;
            break;
        }
    }
    ll A = minus[0] * mi;
    if(plus.empty()){
        if(pl == 0){
            ans = 1;
            sort(all(b));
            rep(i,k){
                ans *= b[i].first;
                ans %= mod;
            }
            ans *= mod - 1;
            ans %= mod;
        }else{
            ans *= pl;
            ans %= mod;
            ans *= modpow(minus[0],mod-2,mod);
            ans %= mod;
        }
        cout << ans << "\n";
        return 0;
    }
    ll B = plus[0] * pl;
    if(A == B && A == 0){
        ans = 1;
        sort(all(b));
        rep(i,k){
            ans *= b[i].first;
            ans %= mod;
        }
        ans *= mod - 1;
        ans %= mod;
        cout << ans << "\n";
    }else{
        if(A > B){
            ans *= mi;
            ans %= mod;
            ans *= modpow(plus[0],mod-2,mod);
            ans %= mod;
        }else{
            ans *= pl;
            ans %= mod;
            ans *= modpow(minus[0],mod-2,mod);
            ans %= mod;
        }
        cout << ans << "\n";
    }
}