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

#include <stdio.h>
#include <stdlib.h>
#define ll long long
#define rep(i, l, r) for (ll i = (l); i < (r); i++)
int upll(const void *a, const void *b) {
  return *(ll *)a < *(ll *)b ? -1 : *(ll *)a > *(ll *)b ? 1 : 0;
}
void sortup(ll *a, int n) { qsort(a, n, sizeof(ll), upll); }
#define MOD 1000000007

ll m[200010], p[200010];
int mcnt, pcnt;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  rep(i, 0, n) {
    int t;
    scanf("%d", &t);
    if (t >= 0)
      p[pcnt++] = t;
    else
      m[mcnt++] = -t;
  }
  // 正の数・負の数をそれぞれソート
  sortup(m, mcnt);
  sortup(p, pcnt);

  // K==Nのとき
  if (k == n) {
    ll ans = 1;
    rep(i, 0, mcnt) ans = ans * (MOD - m[i]) % MOD;
    rep(i, 0, pcnt) ans = ans * p[i] % MOD;
    printf("%lld", ans);
    return 0;
  }

  // Kが奇数かつ負のみのとき
  if (k % 2 == 1 && mcnt == n) {
    ll ans = 1;
    rep(i, 0, k) ans = ans * (MOD - m[i]) % MOD;
    printf("%lld", ans);
    return 0;
  }

  ll ans = 1;
  while (k) {
    // 負の数を2個とる or 正の数を1個とる
    if (k >= 2 && mcnt >= 2 &&
        (pcnt <= 1 || (pcnt >= 2 && m[mcnt - 1] * m[mcnt - 2] >=
                                        p[pcnt - 1] * p[pcnt - 2]))) {
      ans = ans * m[--mcnt] % MOD;
      ans = ans * m[--mcnt] % MOD;
      k -= 2;
    } else {
      ans = ans * p[--pcnt] % MOD;
      k--;
    }
  }
  printf("%lld", ans);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> plus, minus;
  ll zero_cnt = 0;
  rep(i, N) {
    ll a;
    cin >> a;
    if (a > 0) {
      plus.push_back(a);
    } else if (a < 0) {
      minus.push_back(a);
    } else {
      zero_cnt++;
    }
  }
  sort(plus.begin(), plus.end(), greater<ll>()); // 大きい方から
  sort(minus.begin(), minus.end());              // 小さい方から

  ll p_size = plus.size();
  ll m_size = minus.size();

  ll ans = 1;
  if (plus.size() == 0) { // オールマイナス
    if (K % 2 == 1) {
      rep(i, K) {
        ans *= minus.at(N - 1 - i);
        ans %= MOD;
      }
    } else {
      rep(i, K) {
        ans *= minus.at(i);
        ans %= MOD;
      }
    }
  } else if (p_size + m_size < K) {
    ans = 0;
  } else {
    ll p_ind = 0, m_ind = 0, cnt = 0;
    if (K % 2 == 1) {
      p_ind++;
      ans *= plus.at(0);
      ans %= MOD;
      cnt++;
    }
    while (cnt < K) {
      // cout << p_ind << " " << m_ind << endl;
      if (p_size - p_ind >= 2 && m_size - m_ind >= 2) {
        ll p_cand = plus.at(p_ind) * plus.at(p_ind + 1);
        ll m_cand = minus.at(m_ind) * minus.at(m_ind + 1);
        if (p_cand >= m_cand) {
          p_ind += 2;
          ans *= p_cand;
          ans %= MOD;
          cnt += 2;
        } else {
          m_ind += 2;
          ans *= m_cand;
          ans %= MOD;
          cnt += 2;
        }
      } else if (p_size - p_ind >= 2) {
        ans *= plus.at(p_ind);
        ans %= MOD;
        ans *= plus.at(p_ind + 1);
        ans %= MOD;
        p_ind += 2;
        cnt += 2;
      } else if (m_size - m_ind >= 2) {
        ans *= minus.at(m_ind);
        ans %= MOD;
        ans *= minus.at(m_ind + 1);
        ans %= MOD;
        m_ind += 2;
        cnt += 2;
      } else {
        ans *= minus.at(m_ind);
        ans %= MOD;
        ans *= plus.at(p_ind);
        ans %= MOD;
        m_ind++;
        p_ind++;
        cnt += 2;
      }
    }
  }
  if (ans < 0) {
    ans += MOD;
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}