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)

/* Aditya0412 */
#include <bits/stdc++.h>
using namespace std; 
typedef long long int ll;
#define pb push_back 
#define ve vector
#define vii vector < int >
#define vll vector < ll > 
#define pii pair < int , int >
#define pll pair < ll , ll >
#define vpl vector < pll >
#define fi first
#define all(a)      (a).begin(),(a).end()
#define si(x)       (int)((x).size())
#define debug(x)    cout<<#x<<" is "<<(x)<<endl;cout.flush();  
#define se second 
#define endl        '\n'
#define f(i,a,b)    for(int i=a;i<b;i++)
#define mem(a,x)          memset(a,x,sizeof(a))

// === Debug macro starts here ===

int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
    os<<"[";
    for(auto& x:v){os<<x<<", ";}
    return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
    return os<<"{"<<p.first<<", "<<p.second<<"}";
}

// === Debug macro ends here ===


// const ll mod=998244353;
const ll mod = 1000000000+7;
const ll N=1000000+6;
#define M_PI           3.14159265358979323846
void inp(ll *arr,ll n)
{
  for(int i=0;i<n;i++)
    cin>>arr[i];
}
void inp_mat(ll *arr,ll n,ll m)
{
    for(int i=0;i<n;i++)
      for(int j=0;j<m;j++)
        {
          ll x;cin>>x;
          *((arr+i*m) + j)=x;
        }
}
// syntax for inp_mat inp_mat((ll *)arr,n,m);
// int x2[]={+1,-1,+0,+0};
// int y2[]={+0,+0,+1,-1};
ll x,y;
ll mm=1000000007;
void ext_euclid(ll m,ll c)
{
    if(c==0)
    {  
        x=1;
        y=0;
    }
    else
    {
        ext_euclid(c,m%c);
        int temp=x;
        x=y;
        y=temp-(m/c)*y;
    }
}
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //  #ifndef ONLINE_JUDGEc
    //   freopen ("INPUT.txt" , "r" , stdin);
    //   freopen ("OUTPUT.txt", "w" , stdout);
    // #endif 
    int t=1;
    // cin>>t;
    while(t--)
    {
      ll nn,k;cin>>nn>>k;ll arr[nn];inp(arr,nn);
      int cn=0,cp=0;priority_queue< ll > p,n;
      f(i,0,nn)
      {
        if(arr[i]>0)
        {
          cp++;p.push(arr[i]);
        }
        else
        {
          cn++;n.push(abs(arr[i]));
        }
        
      }
      if(k==nn)
      {
        ll ans=1;
        f(i,0,nn)
        {
          ans*=abs(arr[i]);
          ans%=mod;
        }
        if(cn%2)
        {
          ans*=-1;
          ans+=mod;
          ans%=mod;
        }
        cout<<ans;
        return 0;
      }
      if(cp==0)
      {
        if(k%2)
        {
          sort(arr,arr+nn);ll ans=1;
          for(int i=nn-1,c=1;i>=0 and c<=k;i--,c++)
          {
            ans*=abs(arr[i]);
            ans%=mod;
          }
          ans*=-1;
          ans+=mod;
          ans%=mod;
          cout<<ans;
        }
        else
        {
          sort(arr,arr+nn);ll ans=1;
          f(i,0,k)
          {
            ans*=abs(arr[i]);
            ans%=mod;
          }
          cout<<ans;
        }
        
      }
      else if(cn==0)
      {
        ll ans=1;
        sort(arr,arr+nn);
        for(int i=0;i<k;i++)
          {
            ans*=abs(arr[i]);
            ans%=mod;
          }
          cout<<ans;
      }
      else
      {
        ll lp=-1,ln=-1;
        ll kk=k;
        ll ans=1;
        int cnt=0;
        while(kk--)
        {
          if(si(p)==0)
          {
            ll nn=n.top();
            cnt++;
            ans*=nn;
            ans%=mod;
            ln=nn;
            n.pop();
          }
          else if(si(n)==0)
          {
            ll pp=p.top();ans*=pp;ans%=mod;p.pop();lp=pp;
          }
          else
          {
            ll nn=n.top();ll pp=p.top();
            if(pp>=nn)
            {
              ans*=pp;ans%=mod;lp=pp;p.pop();
            }
            else
            {
              ans*=nn;ans%=mod;ln=nn;n.pop();cnt++;
            }
            
          }
          
        }
        if(cnt%2)
        {
            double val1=-1;
            if(si(n))
            {
              val1=n.top();
              val1/=lp;
              if(lp==-1)
              val1=-1;
            }
            double val2=-1;
            if(si(p))
            {
              val2=p.top();
              val2/=ln;

            }
            ans+=mod;ans%=mod;
            if(val1==-1 and val2==-1)
            cout<<ans;
            else if(val1==-1)
            {
              ans*=p.top();
              ans%=mod;
              ext_euclid(ln,mod);
              x+=mod;
              x%=mod;
              ans*=x;
              ans%=mod;
              cout<<ans;
            }
            else if(val2==-1)
            {
              ans*=n.top();
              ans%=mod;
              ext_euclid(lp,mod);
              x+=mod;x%=mod;ans*=x;ans%=mod;cout<<ans;
            }
            else
            {
              if(val1>=val2)
              {
                ans*=n.top();
              ans%=mod;
              ext_euclid(lp,mod);
              x+=mod;x%=mod;ans*=x;ans%=mod;cout<<ans;
              }
              else
              {
                ans*=p.top();
              ans%=mod;
              ext_euclid(ln,mod);
              x+=mod;
              x%=mod;
              ans*=x;
              ans%=mod;
              cout<<ans;
              }
              
            }
            
        }
        else
        {
          ans+=mod;ans%=mod;
          cout<<ans;
        }
        
      }
      
    }    
}