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 REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 1000000007LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n,k,x,y;

//moddivとnCk
ll moddiv(ll a,ll b){
	ll x0=MOD,x1=b,x2,n0=0LL,n1=1LL,n2,t=a%b,m,ans;
	if (t==0LL) return a/b;
		for(int i=0;i<900;i++){
      m=x0/x1;
      x2=x0-x1*m;
      n2=(n0-m*n1)%MOD;
      if (x2==1LL){
        ans=(n2+MOD)%MOD;
        break;
      }
      x0=x1;x1=x2;
      n0=n1;n1=n2;
    }
	return (a+((t*ans)%MOD)*b-t)/b;
}
//剰余の割ざん

//aのn乗 mod c
ll expmod(ll a,ll n){
  ll ans=1LL,aa=a,beki=n;
  for(int i=0;i<64;i++){
    if (beki%2==1LL) ans=ans*aa%MOD;
    aa=aa*aa%MOD;
    beki/=2LL;
    if (beki==0LL)break;
  }
  return ans;
}

//nCk
vi mulmod(500001);//0!,1!,2!,3!,4!,,,,
void first_nCk(){
  mulmod[0]=1LL;
  for(ll i=1;i<500001;i++){
    mulmod[i]=mulmod[i-1]*i%MOD;
  }
}
ll nCk(ll nn,ll kk){
  return moddiv(mulmod[nn],mulmod[kk]*mulmod[nn-kk]%MOD);
}





int main(){
  ll ans=1;
  cin >> n >> k;
  
  ll plcnt=0;
  ll mncnt=0;
  vi a(n);
  REP(i,n){
    cin>>a[i];
    if (a[i]>0)plcnt++;
    if (a[i]<0)mncnt++;
  }
  vi mn(mncnt/2+1,0),pl(plcnt/2+1,0);

  sort(a.begin(), a.end());//低い順
  if (n==k){
    for(ll i=0;i<n;i++){
      ans=ans*a[i];
      ans=(ans%MOD+MOD)%MOD;
    }
    cout<<ans<<endl;
    return 0;
  }

  if (n==mncnt){
    if (k%2==1){
      sort(a.begin(), a.end());//低い順    
      for(ll i=0;i<k;i++){
        ans=ans*a[n-1-i];
        ans=(ans%MOD+MOD)%MOD;
      }
      cout<<ans<<endl;
      return 0;
    }else{
      sort(a.begin(), a.end());//低い順    
      for(ll i=0;i<k;i++){
        ans=ans*a[i];
        ans=(ans%MOD+MOD)%MOD;
      }
      cout<<ans<<endl;
      return 0;
    }
  }



  if (plcnt+mncnt<k){
    cout<<0<<endl;
    return 0;
  }



  if (plcnt+mncnt==k){
    if (mncnt%2==1){
      cout<<0<<endl;
      return 0;
    }else{
      
      for(ll i=0;i<n;i++){
        if (a[i]==0)continue;
        ans=ans*a[i];
        ans=(ans%MOD+MOD)%MOD;
      }
      cout<<ans<<endl;
      return 0;

    }
  }
  


  sort(a.begin(), a.end());//低い順
  
  //1こしかなければ
  if (k==1){
    cout<<a[n-1]<<endl;
    return 0;
  }



  //プラス側から
  for(ll i=0;i<plcnt/2;i++){
    pl[i]=a[n-1-i*2]*a[n-2-i*2]%MOD;
  }
  //マイナス側から
  for(ll i=0;i<mncnt/2;i++){
    mn[i]=a[i*2]*a[i*2+1]%MOD;
  }


  ll wpl=0;
  ll wmn=0;
  ll mx;
  for(ll i=0;i<k/2;i++){
    if (wpl>=plcnt/2){
      mx=mn[wmn];
      wmn++;
    }else{

      if (wmn>=mncnt/2){
        mx=pl[wpl];
        wpl++;
      }else{

          if (pl[wpl]>mn[wmn]){
            mx=pl[wpl];
            wpl++;
          }else{
            mx=mn[wmn];
            wmn++;
          }
      }
    }

    ans=ans*(mx%MOD)%MOD;
  }


  if (k%2==1){
    if (plcnt>wpl*2){
      ans=ans*a[n-1-wpl*2]%MOD;
    }else{
      if (plcnt==0){
        cout<<0<<endl;
      }else{
        ans=moddiv(ans,a[n-1-wpl*2+1]);
        ans=ans*mn[wmn]%MOD;
      }

      return 0;
    }
    
  }








  cout<<ans<<endl;
  return 0;
}