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<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<deque>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg,(x).ed
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep1(i,n) for(ll i=1;i<=(n);i++)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(ll i=(n);i>=1;i--)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
const ll MOD=1000000007;
const ll INF=1000000000000000;
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
ll maxx(ll x,ll y,ll z){return max(max(x,y),z);}
ll minn(ll x,ll y,ll z){return min(min(x,y),z);}
ll gcd(ll x,ll y){if(x%y==0) return y;else return gcd(y,x%y);}
ll lcm(ll x,ll y){return x*(y/gcd(x,y));}

vector<ll> pw2(51,1);vector<ll> pw10(19,1);
int main(){
    ll N,K; cin>>N>>K;
    ll tmp=1;
    vector<ll> A(N);
    vector<ll> minus;
    vector<ll> plus;
    vector<P> absA;
    rep(i,N){
        cin>>A[i];
        if(A[i]<0) minus.pb(A[i]);
        else if(A[i]>0) plus.pb(A[i]);
        
        absA.pb(P(abs(A[i]),A[i]));
    }
    sort(all(absA),greater<P>());
    
    ll minus_size=sz(minus);
    ll plus_size=sz(plus);
    ll zero_size=N-minus_size-plus_size;
    
    ll m=0;
    ll p=0;
    rep(i,K){
        tmp*=absA[i].a;
        tmp%=MOD;
        if(absA[i].b<0) m++;
        else if(absA[i].b>0) p++;
    }
    // 例外
    if(tmp==0){
        cout<<0<<endl;
        return 0;
    }
    if(N==K){
        if(m%2==1) cout<<MOD-tmp<<endl;
        else cout<<tmp<<endl;
        return 0;
    }
    
    if(plus_size==0){
        ll ans=1;
        if(K%2==0){
            sort(all(minus));
            rep(i,K){
                ans*=(-minus[i]);
                ans%=MOD;
            }
            cout<<ans<<endl;
        }
        else{
            if(zero_size==0){
                sort(all(minus),greater<ll>());
                rep(i,K){
                    ans*=(-minus[i]);
                    ans%=MOD;
                }
                cout<<MOD-ans<<endl;
            }
            else{
                cout<<0<<endl;
            }
        }
    }
    else{
        if(m%2==0){
            cout<<tmp<<endl;
        }
        else{
            // のこりピッタリ0
            if(zero_size==N-K){
                cout<<0<<endl;
            }
            //どっちかできる
            else{
                // ソート
                sort(all(plus),greater<ll>());
                sort(all(minus));
                
                ll ans_cnd_1=0;
                ll ans_cnd_2=0;
                // if m奇:(m,p)->(m-1,p+1)or(m+1,p-1)
                // (左)<(右) <-> (0-index) p/m-1 < m/p-1
                if((0 <= m-1)&&(m-1 <= minus_size)&&(0 <= p+1)&&(p+1 <= plus_size)){
                    ans_cnd_1=1;
                    
                    rep(i,m-1){
                        ans_cnd_1*=(-minus[i]);
                        ans_cnd_1%=MOD;
                    }
                    rep(i,p+1){
                        ans_cnd_1*=(plus[i]);
                        ans_cnd_1%=MOD;
                    }
                }
                if((0 <= m+1)&&(m+1 <= minus_size)&&(0 <= p-1)&&(p-1 <= plus_size)){
                    ans_cnd_2=1;
                    
                    rep(i,m+1){
                        ans_cnd_2*=(-minus[i]);
                        ans_cnd_2%=MOD;
                    }
                    rep(i,p-1){
                        ans_cnd_2*=(plus[i]);
                        ans_cnd_2%=MOD;
                    }
                }
                
                // どっちか0なら
                if(ans_cnd_1*ans_cnd_2==0){
                    cout<<max(ans_cnd_1,ans_cnd_2)<<endl;
                }
                else{
                    if((plus[p]*plus[p-1])<(minus[m-1]*minus[m])) cout<<ans_cnd_2<<endl;
                    else cout<<ans_cnd_1<<endl;
                }
            }
        }
        
    }
    
}