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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0, xxx_rep_end = (n); i < xxx_rep_end; ++i)
#define all(x) (x).begin(), (x).end()

template <class T1, class T2> inline void chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
  }
}
template <class T1, class T2> inline void chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
  }
}

constexpr ll mod = 1000000007;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<pair<uint64_t, bool>> V(N);
  ll cnt_p = 0;
  ll cnt_m = 0;

  rep(i, N) {
    ll num;
    cin >> num;
    if (num > 0) {
      V[i] = make_pair(num, true);
      ++cnt_p;
    } else {
      V[i] = make_pair(abs(num), false);
      ++cnt_m;
    }
  }

  ll ans = 1;
  if ((N == K && cnt_m % 2 == 1) || (cnt_p == 0 && K % 2 == 1)) {
    sort(all(V));
    rep(i, K) {
      ans *= V[i].first % mod;
      ans %= mod;
    }
    if (ans == 0) {
      ans = 0;
    } else {
      ans = -ans + mod;
    }
  } else {
    sort(all(V), greater<pair<uint64_t, bool>>());
    ll fugou = 1;
    ll last_p = -1;
    ll last_m = -1;
    rep(i, K) {
      if (V[i].second) {
        last_p = i;
      } else {
        last_m = i;
        fugou *= -1;
      }
    }

    if (fugou == 1) {
      rep(i, K) {
        ans *= V[i].first % mod;
        ans %= mod;
      }
    } else {
      ll next_p = -1;
      ll next_m = -1;
      for (ll i = K; i < N; ++i) {
        if (V[i].second == true) {
          next_p = i;
          break;
        }
      }
      for (ll i = K; i < N; ++i) {
        if (V[i].second == false) {
          next_m = i;
          break;
        }
      }

      ll id_ex = -1;
      ll id_in = -1;
      if (last_p != -1 && last_m != -1 && next_p != -1 && next_m != -1) {
        if (V[last_m].first * V[next_m].first >=
            V[last_p].first * V[next_p].first) {
          id_ex = last_p;
          id_in = next_m;
        } else {
          id_ex = last_m;
          id_in = next_p;
        }
      } else if (last_p == -1 || next_m == -1) {
        id_ex = last_m;
        id_in = next_p;
      } else if (last_m == -1 || next_p == -1) {
        id_ex = last_p;
        id_in = next_m;
      }

      rep(i, K) {
        if (i == id_ex) {
          continue;
        }
        ans *= V[i].first % mod;
        ans %= mod;
      }
      ans *= V[id_in].first % mod;
      ans %= mod;
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0, xxx_rep_end = (n); i < xxx_rep_end; ++i)
#define all(x) (x).begin(), (x).end()

template <class T1, class T2> inline void chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
  }
}
template <class T1, class T2> inline void chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
  }
}

constexpr uint64_t mod = 1000000007;

int main() {
  uint64_t N, K;
  cin >> N >> K;
  vector<uint64_t> minus, plus;
  uint64_t cnt_p = 0;
  uint64_t cnt_m = 0;
  rep(i, N) {
    ll num;
    cin >> num;
    if (num > 0) {
      plus.emplace_back(num);
      ++cnt_p;
    } else if (num <= 0) {
      minus.emplace_back(abs(num));
      ++cnt_m;
    }
  }

  uint64_t ans = 1;

  if (N == K) {
    rep(i, cnt_p) {
      ans *= (plus[i] % mod);
      ans %= mod;
    }
    rep(i, cnt_m) {
      ans *= (minus[i] % mod);
      ans %= mod;
    }

    if (ans == 0) {
      // ans = ans;
    }
    if (cnt_m % 2 == 1) {
      ans = -ans + mod;
    } else {
      // ans = ans;
    }
  } else if (cnt_p == 0 && K % 2 == 1) {
    sort(all(minus));
    rep(i, K) {
      ans *= (minus[i] % mod);
      ans %= mod;
    }
    if (ans == 0) {
      // ans = ans;
    } else {
      ans = -ans + mod;
    }
  } else {
    sort(all(plus), greater<uint64_t>());
    sort(all(minus), greater<uint64_t>());
    uint64_t ip = 0;
    uint64_t im = 0;
    uint64_t mul_p;
    uint64_t mul_m;
    mul_p = (ip + 1 < cnt_p) ? plus[ip] * plus[ip + 1] : 0;
    mul_m = (im + 1 < cnt_m) ? minus[im] * minus[im + 1] : 0;

    while (K > 0) {
      if (K > 1) {
        if (mul_p >= mul_m) {
          ans *= (plus[ip] % mod);
          ans %= mod;
          ip += 1;
          mul_p = (ip + 1 < cnt_p) ? plus[ip] * plus[ip + 1] : 0;
          K -= 1;
        } else {
          ans *= (mul_m % mod);
          ans %= mod;
          im += 2;
          mul_m = (im + 1 < cnt_m) ? minus[im] * minus[im + 1] : 0;
          K -= 2;
        }
      } else if (K == 1) {
        ans *= (plus[ip] % mod);
        ans %= mod;
        K -= 1;
      }
    }
  }
  cout << ans << endl;
}