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;
ll mod=1000000007;
#define rep(i,n) for(int i=0;i<n;i++)
ll exp(ll a,ll n){
  if(n==0) return 1;
  else if(n%2==0) return (exp(a,n/2)*exp(a,n/2))%mod;
  else return (a*exp(a,n-1))%mod;
}
int main() {
  int n,k;
  cin>>n>>k;
  vector<ll>a(n);
  rep(i,n) cin>>a[i];
  sort(a.begin(),a.end());
  vector<ll>minus;
  vector<ll>plus;
  ll zero=0;
  rep(i,n){
    if(a[i]>0) plus.push_back(a[i]);
    else if(a[i]<0) minus.push_back(a[i]);
    else zero++;
  }
  ll m=0;
  ll p=plus.size()-1;
  ll ans=1;
  ll odd=0;
  ll answer=1;
  ll pp=0;
  ll mm=minus.size()-1;
  ll ss=minus.size();
  ll z=zero;
    rep(i,k){
       if(m<ss && p>=0){
         if(abs(minus[m])<=plus[p]){
           ans*=plus[p];
           ans%=mod;
           p--;
         }
         else{
           ans*=abs(minus[m]);
           odd++;
           m++;
           ans%=mod;
         }
       }
      else if(m<ss && p<0){
        ans*=abs(minus[m]);
        ans%=mod;
        m++;
        odd++;
      }
      else if(m>=ss && p>=0){
        ans*=plus[p];
        ans%=mod;
        p--;
      }
      else{
        ans=0;
        z--;
      }
    }
  ll s=plus.size();
  rep(i,k){
       if(pp<s && mm>=0){
         if(abs(minus[mm])<=plus[pp]){
           answer*=abs(minus[mm]);
           answer%=mod;
           mm--;
         }
         else{
           answer*=plus[pp];
           pp++;
           answer%=mod;
         }
       }
      else if(pp<s && mm<0){
        answer*=plus[pp];
           pp++;
           answer%=mod;
      }
      else if(pp>=s && mm>=0){
        answer*=abs(minus[mm]);
           answer%=mod;
           mm--;
      }
      else{
        answer=0;
      }
    }
  if(odd%2==0) cout<<ans<<endl;
  else if(z!=zero) cout<<0<<endl;
  else{
    m--;
    ll y=exp(abs(minus[m]),mod-2);
    ans*=y;
    ans%=mod;
    if(p>=0) {
      ans*=plus[p];
      ans%=mod;
      cout<<ans<<endl;
    }
    else if(z!=0) ans=0;
    else {
      answer*=-1;
      answer%=mod;
      if(answer!=0) answer+=mod;
      cout<<answer<<endl;
    }
  }
}