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: Runtime Error)

#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<bitset>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

int main(){
  ll N , K; cin >> N >> K;
  vector<ll> a(N); rep(i,N) cin >> a[i];
  vector<ll> b(N); rep(i,N) b[i] = abs(a[i]);
  ll ans = 1;

  // マイナスにしかできない時に関して
  bool monly = false;
  ll mcnt = 0;
  rep(i,N){
    if(a[i] > 0) mcnt++;
  }
  for(ll i = K; i >= K - mcnt; i--){
    if(i % 2 == 0) monly = true;
  }
  if(!monly){
    SORT(b);
    rep(i,N){
      if(a[i] == 0){
        cout << 0 << endl;
        return 0;
      }
    }
    rep(i,K){
      ans *= b[i];
      ans %= MOD ;
    }
    cout << -ans + MOD << endl;
    return 0;
  }

  // minusがない
  ans = 1;
  if(mcnt == N){
    SORT(a); reverse(a.begin() , a.end());
    rep(i,K){
      ans *= a[i];
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }

  // 以降plusにできる時
  vector<ll> p;
  vector<ll> m;
  rep(i,N){
    if(a[i] > 0) p.pb(a[i]);
    else m.pb(a[i]);
  }
  SORT(p); reverse(p.begin() , p.end());
  rep(i,m.size()){
    m[i] = abs(m[i]);
  }
  SORT(m); reverse(m.begin() , m.end());

  ans = 1;
  // 境目を求める
  ll tmp = 1;
  ll cnt = 0;
  m.pb(1); p.pb(1);
  vector<ll> p2;
  vector<ll> m2;

  if(K % 2 == 1) p2.pb(p[0]);

  for(ll i = K % 2; i < p.size() ; i += 2){
    p2.pb(p[i] * p[i + 1]);
  }
  for(ll i = 0; i < m.size() ; i += 2){
    m2.pb(m[i] * m[i + 1]);
  }

  ll check = 0;
  rep(i,m2.size()){
    if( (K / 2 - i) < 0 ) break;
    if(m2[i] > p2[K / 2 - i]){
      check++;
      cout << m2[i] << " " << p2[K / 2 - i] << endl;
    }
    else break;
  }

  // cout << "check is " <<  check << endl;


  ans = 1;
  rep(i,check){
    ans *= m2[i];
    ans %= MOD;
  }
  rep(i,(K - check * 2)){
    ans *= p2[i];
    ans %= MOD;
  }
  cout << ans << endl;
}