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;
using ll = long long;

const ll MOD = 1e9 + 7;

ll modPow(ll a, ll x) {
  if (x == 0) return 1;
  if (x == 1) return a;
  ll temp = modPow(a, x / 2);
  ll ans = (temp * temp) % MOD;
  if (x % 2 == 1) ans = (ans * a) % MOD;
  return ans;
}

ll modInv(ll a) {
  return modPow(a, MOD - 2);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll N; cin >> N;
  ll K; cin >> K;

  ll cnt_p = 0, cnt_0 = 0, cnt_n = 0;
  vector<ll> A(N, 0);
  vector<pair<ll, ll>> ab(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] > 0) {
      cnt_p++;
      ab[i] = {abs(A[i]), 1};
    } else if (A[i] == 0) {
      cnt_0++;
      ab[i] = {abs(A[i]), 0};
    } else if (A[i] < 0) {
      cnt_n++;
      ab[i] = {abs(A[i]), -1};
    }
  }
  sort(ab.rbegin(), ab.rend());

  ll new_cnt_p = 0, new_cnt_0 = 0, new_cnt_n = 0;
  ll new_last_p = 0, new_last_n = 0;
  for (ll i = 0; i < K; i++) {
    if (ab[i].second == -1) {
      new_cnt_n++;
      new_last_n = ab[i].first;
    } else {
      new_last_p = ab[i].first;
    }
  }

  if (new_cnt_n % 2 == 0 || N == K) {
    ll ans = 1;
    for (ll i = 0; i < K; i++) {
      ans = (ans * ab[i].first * ab[i].second) % MOD;
      ans = (ans + MOD) % MOD;
    }
    cout << ans << '\n';
    return 0;
  }

  if (cnt_p == 0 && cnt_0 > 0) {
    cout << 0 << '\n';
    return 0;
  }
  if (cnt_p == 0 && cnt_0 == 0) {
    sort(ab.begin(), ab.end());
    ll ans = 1;
    for (ll i = 0; i < K; i++) {
      ans = (ans * ab[i].first) % MOD;
      ans = (-ans + MOD) % MOD;
    }
    cout << ans << '\n';
    return 0;
  }

  ll next_p = 0, next_n = 0, prev_p = 0, prev_n = 0;
  if (new_cnt_p < cnt_p) {
    for (ll i = K; i < N; i++) {
      if (ab[i].second > 0) {
        next_p = ab[i].first;
        break;
      }
    }
  }
  if (new_cnt_n < cnt_n) {
    for (ll i = K; i < N; i++) {
      if (ab[i].second < 0) {
        next_n = ab[i].first;
        break;
      }
    }
  }


  ll ans = 1;
  for (ll i = 0; i < K; i++) {
    ans = (ans * ab[i].first * ab[i].second) % MOD;
  }

  if (next_p * new_last_p >= next_n * new_last_n && new_cnt_p < cnt_p) {
    new_last_n = (-new_last_n + MOD) % MOD;
    ans = (ans * modInv(new_last_n)) % MOD;
    ans = (ans * next_p) % MOD;
  } else {
    new_last_p = (new_last_p + MOD) % MOD;
    ans = (ans * modInv(new_last_p)) % MOD;
    ans = (ans * -next_n) % MOD;
  }

  ans = (ans + MOD) % MOD;
  cout << ans << '\n';

  return 0;
}