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>
  #pragma GCC target ("avx2")
  #pragma GCC optimization ("O3")
  #pragma GCC optimization ("unroll-loops")
  using ll = long long;
  using ld = long double;
  #define whatis(x) cout << "> "<<#x << " ==> " << x << endl;
  #define br cout<< "\n";
  #define endl "\n"
  #define vi vector<int>
  #define vii vector<ll>
  #define pll pair<ll,ll>
  #define vpll vector<pll>
  #define pii pair<int,int>
  #define pcl pair<char,ll>
  #define pb push_back
  #define eb emplace_back
  #define mp make_pair
  #define lb lower_bound
  #define ub upper_bound
  #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
  #define all(v) begin(v),end(v)
  #define minv(a) *min_element(all(a))
  #define maxv(a) *max_element(all(a))
  #define outputv(a) for(auto i:a) cout<<i<<" ";br;
  #define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  #define outputvp(a) for(auto j:a) {cout<<j.first<<" " << j.second << ",",br;}
  #define T ll t;cin>>t;while(t--)
  using namespace std;

  const ll mod = 1000000007;
  const ll inf = 1000000000000000000;
  const ll rk = 256;
  const ld PI = 3.141592653589793;
  

  int dr[]={1,-1,0,0};
  int dc[]={0,0,1,-1};

  // ll isPrime[1000001];

  // void primeDivisors(map<ll,ll> &mapi, ll n){
  //   while(n > 1){
  //     if(isPrime[n] == -1){
  //       mapi[n]+=1;
  //       break;
  //     }
  //     else{
  //       mapi[isPrime[n]]+=1;
  //       n/=isPrime[n]; 
  //     }
  //   }
  // }


// void seive(){
//   ll maxn = 10000001;
//   isPrime[0] = 0; isPrime[1] = 0;
//   rep(i,2,maxn) isPrime[i] = -1;
//   for(int i=2; i*i < maxn; i++){
//     if(isPrime[i] == -1){
//       for(int j=i*i; j < maxn; j+=i){
//         if(isPrime[j] == -1){
//           isPrime[j] = i;
//         }
//       } 
//     }
//   }

// }

 ll power( ll base, ll power, ll mod){
    ll res = 1;
    while( power ){
      if(power%2) res= (res*base)%mod, power--;
      else base= (base * base)%mod, power/=2;
    }

    return res;
  }

    void inout(){
      // seive();
      #ifndef ONLINE_JUDGE
          // for getting input from input.txt
          freopen("/home/logan/contest/ongoing/input.txt", "r", stdin);
          // for writing output to output.txt
          freopen("/home/logan/contest/ongoing/output.txt", "w", stdout);
      #endif
  }

  long long int modi(long long int a, long long int b) {
    long long int ret = a % b;
    if (ret < 0)
        ret += b;
    return ret;
}

  

  int main(){
     FastIO
     // seive();
    //inout();

    
    ll n,k; cin >> n >> k;
    // whatis(n);whatis(k);
    vii a,b;
    rep(i,0,n){
      ll x; cin >> x;
      if(x >= 0 ) a.pb(x);
      else b.pb(x);
    }    
 ll ans = 1;
      ll c1 = 0;
      ll c2 = 0;
    if(a.size() == 0){
      sort(all(b), greater<ll>());
      // outputv(b);
      // whatis(k);
      rep(i,0,k){
        // whatis(b[k]);
      ans = ans%mod * b[i]%mod;
      }
      // whatis(ans);
      // cout << -6%mod << endl;
      cout << modi(ans,mod)  << endl;
    }else{


    sort(all(a), greater<ll>());
    sort(all(b));
      

     
    while( k > 0 && c1 < a.size() && c2 < b.size()){
          // cout << "here\n";
      if( k >= 2 && c2+1 < b.size()){
        if( ans*(b[c2]*b[c2+1]) > ans*a[c1]){
          ans = ans%mod * (b[c2]%mod) * (b[c2+1]%mod);
          // whatis(ans);
          c2+=2;
          k-=2;
        }else{
          ans = ans%mod * a[c1]%mod;
          c1+=1;
          k-=1;
        }
      
      }else{
        // cout << "here\n";
        ans = ans%mod * a[c1]%mod;
        c1+=1;
        k-=1;
      }
    }


    while( k > 0 && c1 < a.size()){
      // cout << "here" << endl;
      ans = ans%mod * a[c1]%mod;
      c1+=1;
      k-=1;
      // whatis(c1);
      // whatis(k);
    }

    while( k > 0 && c2 < b.size()){
      ans = ans%mod * b[c2]%mod * b[c2+1]%mod;
      c2+=2;
      k-=2;
    }

    
    cout << ans%mod << endl;
  }






  return 0;
  }