Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rng(a) a.begin(), a.end()
using namespace std;
using ll = long long;
const int INF = 1001001001;
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> p, m;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x >= 0)
      p.push_back(x);
    else
      m.push_back(-1 * x);
  }
  sort(p.rbegin(), p.rend());
  sort(m.rbegin(), m.rend());
  p.push_back(-1);
  m.push_back(-1);
  ll pp = 0, mm = 0; // ppは正の数で使う個数
  while (k > 0) {
    if (p[pp] >= m[mm])
      pp++;
    else
      mm++;
    k--;
  }
  mint ans = 1;
  if (p.size() == 1 && mm % 2 == 1) { // 答えは必ず負になる
    m.pop_back();
    sort(m.begin(), m.end());
  } else if (pp > 0 && mm % 2 == 1) {
    if (p[pp - 1] * p[pp] >= m[mm - 1] * m[mm] && pp < (ll)p.size() - 1) {
      pp++;
      mm--;
    } else if (p[pp - 1] * p[pp] <= m[mm - 1] * m[mm] &&
               mm < (ll)m.size() - 1) {
      pp--;
      mm++;
    }
  } else if (pp == 0 && mm % 2 == 1 && pp < (ll)p.size() - 1) {
    pp++;
    mm--;
  }
  rep(i, pp) ans *= p[i];
  rep(i, mm) ans *= -1 * m[i];
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rng(a) a.begin(), a.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<ll, int>> a(n);
  rep(i, n) {
    ll x;
    cin >> x;
    if (x < 0) {
      a[i] = make_pair(-1 * x, -1);
    } else
      a[i] = make_pair(x, 1);
  }
  sort(rng(a));
  reverse(rng(a));
  ll ans = 1;
  ll mi = 0;
  ll pl = 0;
  rep(i, k) {
    ans %= MOD;
    ans *= a[i].first * a[i].second;
    if (a[i].second > 0)
      pl = a[i].first;
    else
      mi = a[i].first;
  }

  // if(ans>=0||k==n)
  if (ans < 0 && k < n) {
    ll mm = 0, pp = 0;
    for (int i = k; i < n; i++) {
      if (a[i].second >= 0) {
        pp = a[i].first;
        break;
      }
    }
    for (int i = k; i < n; i++) {
      if (a[i].second < 0) {
        mm = a[i].first;
        break;
      }
    }
    if (pl * mm >= mi * pp)
      ans = ans / mi * mm;
    else
      ans = ans / pl * pp;
  }
  if (ans < 0) {
    ans = 1;
    for (int i = n - 1; i >= n - k; i--) {
      ans *= a[i].first * a[i].second % MOD;
    }
    ans += MOD;
  }
  ans %= MOD;
  cout << ans << endl;
  return 0;
}