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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

const ll MOD = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> data(n);
  vector<ll> pl, mi;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x > 0)
      data[i] = make_pair(x, 1), pl.pb(x);
    else if (x < 0)
      data[i] = make_pair(-x, -1), mi.pb(-x);
    else if (x == 0)
      data[i] = make_pair(0, 0);
  }
  sort(all(data), greater<pair<ll, ll>>());
  sort(all(pl), greater<ll>());
  ll pp = pl.size();
  sort(all(mi), greater<ll>());
  ll mm = mi.size();

  ll ans = 1, num = 1;

  if (n == k) {
    rep(i, k) ans *= data[i].first, ans %= MOD, num *= data[i].second;
    cout << (num >= 0 ? ans : MOD - ans) << endl;
    return 0;
  }

  if (pp == 0 && k % 2 == 1) {
    if (mm < n) {
      cout << 0 << endl;
      return 0;
    }
    rep(i, k) ans *= mi[n - 1 - i], ans %= MOD;
    cout << MOD - ans << endl;
    return 0;
  }

  ll ppc = 0, mmc = 0;
  rep(i, k) {
    ans *= data[i].first, ans %= MOD;
    num *= data[i].second;
    if (data[i].second == 1)
      ppc++;
    else if (data[i].second == -1)
      mmc++;
  }
  if (num >= 0) {
    cout << ans << endl;
    return 0;
  }

  if (pp == ppc && mm == mmc) {
    cout << 0 << endl;
    return 0;
  }

  ll ans2 = 1;

  if (ppc == 0) {
    rep(i, mmc - 1) ans2 *= mi[i], ans2 %= MOD;
    cout << ans2 * pl[0] % MOD << endl;
    return 0;
  }

  rep(i, ppc - 1) ans2 *= pl[i], ans2 %= MOD;
  rep(i, mmc - 1) ans2 *= mi[i], ans2 %= MOD;

  ll save1 = 0, save2 = 0;
  if (pp > ppc)
    save1 = pl[ppc - 1] * pl[ppc];
  if (mm > mmc)
    save2 = mi[mmc - 1] * mi[mmc];

  if (save1 > save2)
    ans2 *= (pl[ppc - 1] * pl[ppc] % MOD), ans2 %= MOD;
  else
    ans2 *= (mi[mmc - 1] * mi[mmc] % MOD), ans2 %= MOD;
  cout << ans2 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

const ll MOD = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> data(n);
  vector<ll> pl, mi;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x > 0)
      data[i] = make_pair(x, 1), pl.pb(x);
    else if (x < 0)
      data[i] = make_pair(-x, -1), mi.pb(-x);
    else
      data[i] = make_pair(0, 0);
  }
  sort(all(data), greater<pair<ll, ll>>());
  sort(all(pl), greater<ll>());
  ll pp = pl.size();
  sort(all(mi), greater<ll>());
  ll mm = mi.size();

  ll ans = 1, num = 1;

  if (n == k) {
    rep(i, n) ans *= data[i].first, ans %= MOD, num *= data[i].second;
    cout << (num >= 0 ? ans : MOD - ans) << endl;
    return 0;
  }

  if (pp == 0 && k % 2 == 1) {
    if (pp + mm < n) {
      cout << 0 << endl;
      return 0;
    }
    rep(i, k) ans *= mi[n - i - i], ans %= MOD;
    cout << MOD - ans << endl;
    return 0;
  }

  ll ppc = 0, mmc = 0;
  rep(i, k) {
    ans *= data[i].first, ans %= MOD;
    num *= data[i].second;
    if (data[i].second == 1)
      ppc++;
    else
      mmc++;
  }
  if (num == 1) {
    cout << ans << endl;
    return 0;
  }
  if (pp == ppc && mm == mmc) {
    cout << 0 << endl;
    return 0;
  }

  ll ans2 = 1;
  rep(i, ppc - 1) ans2 *= pl[i], ans2 %= MOD;
  rep(i, mmc - 1) ans2 *= mi[i], ans2 %= MOD;
  ll save1 = 0, save2 = 0;
  if (pp > ppc)
    save1 = mi[mmc - 1] * pl[ppc];
  if (mm > mmc)
    save2 = pl[ppc - 1] * mi[mmc];
  if (save1 > save2)
    ans2 *= (save1 % MOD), ans2 %= MOD;
  else
    ans2 *= (save2 % MOD), ans2 %= MOD;
  cout << ans2 << endl;
  return 0;
}