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 <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <numeric>
#include <iomanip>

#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT { INIT(){
  cin.tie(0); ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
}}INIT;

struct mint {
  int val;
  constexpr mint(int v = 0) noexcept : val(v % MOD) {
    if (val < 0) val += MOD;
  }
  constexpr int getMOD() { return MOD; }
  constexpr mint operator - () const noexcept {
    return val ? MOD - val : 0;
  }
  constexpr mint operator + (const mint& r) const noexcept { return mint(*this) += r; }
  constexpr mint operator - (const mint& r) const noexcept { return mint(*this) -= r; }
  constexpr mint operator * (const mint& r) const noexcept { return mint(*this) *= r; }
  constexpr mint operator / (const mint& r) const noexcept { return mint(*this) /= r; }
  constexpr mint& operator += (const mint& r) noexcept {
    val += r.val;
    if (val >= MOD) val -= MOD;
    return *this;
  }
  constexpr mint& operator -= (const mint& r) noexcept {
    val -= r.val;
    if (val < 0) val += MOD;
    return *this;
  }
  constexpr mint& operator *= (const mint& r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr mint& operator /= (const mint& r) noexcept {
    int a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      int t = a / b;
      a -= t * b; swap(a, b);
      u -= t * v; swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0) val += MOD;
    return *this;
  }
  constexpr bool operator == (const mint& r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator != (const mint& r) const noexcept {
    return this->val != r.val;
  }
  friend ostream& operator << (ostream &os, const mint& x) noexcept {
    return os << x.val;
  }
  friend istream& operator >> (istream &is, mint& x) noexcept {
    return is >> x.val;
  }
  friend constexpr mint powMod(const mint &a, int n) noexcept {
    if (n == 0) return 1;
    auto t = powMod(a, n / 2);
    t = t * t;
    if (n & 1) t = t * a;
    return t;
  }
};


signed main() {
  int N, K;
  cin >> N >> K;

  vector<int> plus, minus;
  int zero = 0;
  REP(i, N){
    int A;
    cin >> A;
    if(A > 0){
      plus.emplace_back(A);
    }else if(A < 0){
      minus.emplace_back(A);
    }else{
      zero++;
    }
  }

  bool sei = false;
  if(K <= plus.size()) sei = true;
  int req = K-plus.size();
  if(req%2) req++;
  if(K%2){
    if(plus.size() > 0){
      if(req <= minus.size()) sei = true;
    }
  }else{
    if(req <= minus.size()) sei = true;
  }

  mint ans = 1;
  if(sei){
    sort(ALL(plus), greater<>());
    sort(ALL(minus));

    int p = 0, m = req;
    REP(i, req){
      ans *= minus[i];
      K--;
    }
    REP(i, K){
      if(i != K-1 && m+1 < minus.size() && p+1 < plus.size()){
        int a = plus[p]*plus[p+1];
        int b = minus[m]*minus[m+1];
        if(a <= b){
          ans *= b;
          m += 2;
        }else{
          ans *= a;
          p += 2;
        }
        K--;
      }else{
        ans *= plus[p++];
      }
    }
    cout << ans << "\n";

  }else{
    if(zero) cout << 0 << "\n";
    else{
      sort(ALL(plus));
      sort(ALL(minus), greater<>());
      int p = 0, m = 0;
      REP(i, K){
        int a = INF, b = INF;
        if(p < plus.size()) a = plus[p];
        if(m < minus.size()) b = minus[m];
        if(a < abs(b)){
          ans *= a;
          p++;
        }else{
          ans *= b;
          m++;
        }
      }
      cout << ans << "\n";
    }
  }

  return 0;
}