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)

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

template<unsigned mod=1000000007>struct fp {
   int v; static unsigned get_mod(){return mod;}
   int inv() const{
      int tmp,a=v,b=mod,x=1,y=0;
      while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
      if(x<0){x+=mod;} return x;
   }
   fp():v(0){}
   fp(ll x):v(x>=0?x%mod:mod+(x%mod)){}
   fp operator-()const{return fp(-v);}
   fp pow(ll t){fp res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;} return res;}
   fp& operator+=(const fp& x){if((v+=x.v)>=(int)mod)v-=mod; return *this;}
   fp& operator-=(const fp& x){if((v+=mod-x.v)>=(int)mod)v-=mod; return *this;}
   fp& operator*=(const fp& x){v=ll(v)*x.v%mod; return *this;}
   fp& operator/=(const fp& x){v=ll(v)*x.inv()%mod; return *this;}
   fp operator+(const fp& x)const{return fp(*this)+=x;}
   fp operator-(const fp& x)const{return fp(*this)-=x;}
   fp operator*(const fp& x)const{return fp(*this)*=x;}
   fp operator/(const fp& x)const{return fp(*this)/=x;}
   bool operator==(const fp& x)const{return v==x.v;}
   bool operator!=(const fp& x)const{return v!=x.v;}
}; using Fp=fp<>;

int main(){
   int n,k; cin>>n>>k;
   vector<int> a(n);
   rep(i,0,n)cin>>a[i];
   if(n==k){
      Fp res=1;
      rep(i,0,n)res*=a[i];
      cout<<res.v<<endl; return 0;
   }
   sort(ALL(a));
   if(a.back()<0 and k%2==1){
      Fp res=1;
      rep(i,n-k,n)res*=a[i];
      cout<<res.v<<endl; return 0;
   }
   vector<int> pos,neg;
   rep(i,0,n){
      if(a[i]>=0)pos.push_back(a[i]);
      else neg.push_back(-a[i]);
   }
   sort(ALL(pos),greater<int>());
   sort(ALL(neg),greater<int>());
   Fp res=1;
   int pi=0,ni=0;
   while(k){
      if(k==1){
         res*=pos[pi]; k--;
         continue;
      }
      if(neg.size()-ni<=1){
         
         res*=1LL*pos[pi]*pos[pi+1];
         pi+=2; k-=2;
      }
      else if(pos.size()-pi==0){
         res*=1LL*neg[ni]*neg[ni+1];
         ni+=2; k-=2;
      }
      else{
         ll P=1LL*pos[pi]*pos[pi+1];
         ll Q=1LL*neg[ni]*neg[ni+1];
         if(P<Q){
            res*=Q; ni+=2; k-=2;
         }
         else{
            res*=pos[pi]; pi++; k--;
         }
      }
   }
   cout<<res.v<<endl;
   return 0;
}