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>
using namespace std;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert
#undef LOCAL
#ifdef LOCAL
template <class T, class S>
ostream &operator<<(ostream &str, const pair<T, S> &p) {
  return (str << "{" << p.fi << ", " << p.se << "}");
}
template <template <class...> class Container, class... T>
ostream &operator<<(ostream &str, const Container<T...> &vec) {
  str << '[';
  for (auto it = vec.begin(); it != vec.end(); it++) {
    if (it != vec.begin())
      str << ", ";
    str << *it;
  }
  str << ']';
  return str;
}
#define var(x) "[" << #x << ": " << x << "] "
struct dbg {
  dbg() { cout << "  "; }
  ~dbg() { cout << endl; }
  template <class T> dbg &operator<<(T oth) {
    cout << oth;
    return *this;
  }
};
#else
#define var(x) 0
struct dbg {
  template <class T> dbg &operator<<(T oth) { return *this; }
};
#endif
using ll = long long;
using ull = unsigned long long;
using ld = long double;
int main(int argc, char **argv) {
  ios::sync_with_stdio(false), cin.tie(0);
  auto solve = [&]() {
    int n, k;
    cin >> n >> k;
    vector<ll> pos, neg;
    for (int i = 0; i < n; i++) {
      ll d;
      cin >> d;
      if (d >= 0)
        pos.pb(d);
      else
        neg.pb(-d);
    }
    sort(neg.rbegin(), neg.rend());
    sort(pos.rbegin(), pos.rend());
    const ll mod = (ll)(1e9 + 7);
    
    if (n == k) {
      ll m1 = ll(1e9 + 6);
      ll ans = 1;
      for (auto g : pos)
        ans = (ans * g) % mod;
      for (auto g : neg)
        ans = (((ans * g) % mod) * m1) % mod;
      cout << ans << '\n';
      return;
    }

    if (pos.empty()) {
      ll ans = 1;
      if (k & 1) {
        for (int i = 0; i < k; i++)
          ans = (ans * neg[int(neg.size()) - 1 - i]) % mod;
        if (ans)
          ans = mod - ans;
      } else {
        for (int i = 0; i < k; i++)
          ans = (ans * neg[i]) % mod;
      }
      cout << ans << '\n';
    } else {
      int pos_taken = min(k, (int)pos.size());
      int neg_taken = k - pos_taken;
      if ((neg_taken & 1) && pos_taken != pos.size())
        --neg_taken, ++pos_taken;
      dbg() << var(pos_taken) << var(neg_taken);
      while (pos_taken >= 2 && neg_taken + 1 < (int)neg.size()) {
        ll pos_give = pos[pos_taken - 1] * pos[pos_taken - 2];
        dbg() << var(neg) << var(neg_taken);
        ll neg_give = neg[neg_taken] * neg[neg_taken + 1];
        if (neg_give < pos_give)
          break;
        pos_taken -= 2, neg_taken += 2;
      }
      ll ans = 1;

      for (int i = 0; i < pos_taken; i++)
        ans = (ans * pos[i]) % mod;
      for (int i = 0; i < neg_taken; i++)
        ans = (ans * neg[i]) % mod;
      cout << ans << '\n';
    }
  };
  int t;
  t = 1;
  while (t--)
    solve();
  return 0;
}