Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr ll INF = 3000000000000000000;

constexpr int MOD = 1000000007;
struct ModInt {
  ll x;
  ModInt(ll a) : x((a % MOD + MOD) % MOD) {}
  ModInt operator-() const { return ModInt(-x); }
  ModInt &operator+=(const ModInt &a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &a) {
    if ((x -= a.x) < 0)
      x += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  ModInt operator+(const ModInt &a) const {
    ModInt res(*this);
    return res += a;
  }
  ModInt operator-(const ModInt &a) const {
    ModInt res(*this);
    return res -= a;
  }
  ModInt operator*(const ModInt &a) const {
    ModInt res(*this);
    return res *= a;
  }
  ModInt pow(ll t) const {
    if (!t)
      return 1;
    ModInt a = pow(t / 2);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  ModInt inv() const { return pow(MOD - 2); }
  ModInt &operator/=(const ModInt &a) { return (*this) *= a.inv(); }
  ModInt operator/(const ModInt &a) const {
    ModInt res(*this);
    return res /= a;
  }
  friend istream &operator>>(istream &is, ModInt &a) {
    is >> a.x;
    return is;
  }
  friend ostream &operator<<(ostream &os, const ModInt &a) {
    os << a.x;
    return os;
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];

  vector<ll> mai, zer, plu;
  REP(i, n) {
    if (a[i] > 0)
      plu.push_back(a[i]);
    if (a[i] == 0)
      zer.push_back(a[i]);
    if (a[i] < 0)
      mai.push_back(a[i]);
  }

  sort(ALL(mai));
  sort(ALL(plu));
  reverse(ALL(plu));
  int ans = -1;
  REP(i, n + 1) {
    if (i & 1)
      continue;
    if (i > SZ(mai))
      continue;
    if (i > k)
      continue;
    if (i + SZ(plu) < k)
      continue;
    if (ans == -1) {
      ans = i;
    } else {
      if (mai[i - 2] * mai[i - 1] > plu[k - i] * plu[k - i + 1]) {
        ans = i;
      } else {
        break;
      }
    }
  }
  if (ans == -1) {
    if (SZ(zer)) {
      cout << 0 << "\n";
    } else {
      reverse(ALL(mai));
      REP(i, n + 1) {
        if (!(i & 1))
          continue;
        if (i > SZ(mai))
          continue;
        if (i > k)
          continue;
        if (i + SZ(plu) < k)
          continue;
        if (ans == -1) {
          ans = i;
        } else {
          if (mai[i - 2] * mai[i - 1] < plu[k - i] * plu[k - i + 1]) {
            ans = i;
          } else {
            break;
          }
        }
      }
    }
  }
  ModInt answer(1);
  REP(i, ans) { answer *= ModInt(mai[i]); }
  REP(i, k - ans) { answer *= ModInt(plu[i]); }
  cout << answer << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr ll INF = 3000000000000000000;

constexpr int MOD = 1000000007;
struct ModInt {
  ll x;
  ModInt(ll a) : x((a % MOD + MOD) % MOD) {}
  ModInt operator-() const { return ModInt(-x); }
  ModInt &operator+=(const ModInt &a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &a) {
    if ((x -= a.x) < 0)
      x += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  ModInt operator+(const ModInt &a) const {
    ModInt res(*this);
    return res += a;
  }
  ModInt operator-(const ModInt &a) const {
    ModInt res(*this);
    return res -= a;
  }
  ModInt operator*(const ModInt &a) const {
    ModInt res(*this);
    return res *= a;
  }
  ModInt pow(ll t) const {
    if (!t)
      return 1;
    ModInt a = pow(t / 2);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  ModInt inv() const { return pow(MOD - 2); }
  ModInt &operator/=(const ModInt &a) { return (*this) *= a.inv(); }
  ModInt operator/(const ModInt &a) const {
    ModInt res(*this);
    return res /= a;
  }
  friend istream &operator>>(istream &is, ModInt &a) {
    is >> a.x;
    return is;
  }
  friend ostream &operator<<(ostream &os, const ModInt &a) {
    os << a.x;
    return os;
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];

  vector<ll> mai, zer, plu;
  REP(i, n) {
    if (a[i] > 0)
      plu.push_back(a[i]);
    if (a[i] == 0)
      zer.push_back(a[i]);
    if (a[i] < 0)
      mai.push_back(a[i]);
  }

  sort(ALL(mai));
  sort(ALL(plu));
  reverse(ALL(plu));
  int ans = -1;
  REP(i, n + 1) {
    if (i & 1)
      continue;
    if (i > SZ(mai))
      continue;
    if (i > k)
      continue;
    if (i + SZ(plu) < k)
      continue;
    if (ans == -1) {
      ans = i;
    } else {
      if (mai[i - 2] * mai[i - 1] > plu[k - i] * plu[k - i + 1]) {
        ans = i;
      } else {
        break;
      }
    }
  }
  if (ans == -1) {
    if (SZ(zer)) {
      cout << 0 << "\n";
      return 0;
    } else {
      reverse(ALL(mai));
      REP(i, n + 1) {
        if (!(i & 1))
          continue;
        if (i > SZ(mai))
          continue;
        if (i > k)
          continue;
        if (i + SZ(plu) < k)
          continue;
        if (ans == -1) {
          ans = i;
        } else {
          if (mai[i - 2] * mai[i - 1] < plu[k - i] * plu[k - i + 1]) {
            ans = i;
          } else {
            break;
          }
        }
      }
    }
  }
  ModInt answer(1);
  REP(i, ans) { answer *= ModInt(mai[i]); }
  REP(i, k - ans) { answer *= ModInt(plu[i]); }
  cout << answer << "\n";
  return 0;
}