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

#include <bits/stdc++.h>
using namespace std;

template <int64_t N> class modint {
  int64_t x;

public:
  modint(int64_t x = 0) : x((x % N + N) % N) {}
  modint operator-() const { return modint(-x); }
  modint &operator+=(const modint &a) {
    if ((x += a.x) >= N)
      x -= N;
    return *this;
  }
  modint &operator-=(const modint &a) {
    if ((x += N - a.x) >= N)
      x -= N;
    return *this;
  }
  modint &operator*=(const modint &a) {
    (x *= a.x) %= N;
    return *this;
  }
  modint operator+(const modint &a) const { return modint(*this) += a; }
  modint operator-(const modint &a) const { return modint(*this) -= a; }
  modint operator*(const modint &a) const { return modint(*this) *= a; }
  modint pow(int64_t t) const {
    if (not t)
      return 1;
    modint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  modint inv() const { return pow(N - 2); }
  modint &operator/=(const modint &a) { return (*this) *= a.inv(); }
  modint operator/(const modint &a) const { return modint(*this) /= a; }
  friend ostream &operator<<(ostream &os, const modint &m) {
    os << m.x;
    return os;
  }
};

const int64_t mod = 1e9 + 7;
using mint = modint<mod>;

int main() {
  int n, k;
  cin >> n >> k;
  mint ans = 1;
  vector<int64_t> c;
  vector<pair<int64_t, bool>> d(n);
  for (auto i = 0; i < n; i++) {
    int64_t a;
    cin >> a;
    if (a < 0)
      c.push_back(a);
    d.push_back({abs(a), (a < 0)});
  }

  sort(c.begin(), c.end(), greater<>());
  if (k % 2 and (int) c.size() == n) { // 選ぶ個数が奇数かつ負の値しかない
    for (auto i = 0; i < k; i++)
      ans *= c[i];
    cout << ans << endl;
    return 0;
  }

  sort(d.begin(), d.end(), greater<>());
  bool minus = false;
  pair<int64_t, int64_t> prev(-1, -1), next(-1, -1);
  for (auto i = 0; i < k; i++) {
    ans *= ((d[i].second) ? -1 : 1) * d[i].first;
    if (d[i].second)
      minus = not minus, prev.second = -d[i].first;
    else
      prev.first = d[i].first;
  }
  if (k == n) {
    cout << ans << endl;
    return 0;
  }

  if (minus) {
    auto cnt = bitset<2>(3);
    for (auto i = k; i < n; i++) {
      if (d[i].second and cnt[0])
        next.second = -d[i].first, cnt[0] = false;
      else if (not d[i].second and cnt[1])
        next.first = d[i].first, cnt[1] = false;
    }
    if (prev.first == -1 or next.second == -1)
      ans /= prev.second, ans *= next.first;
    else if (prev.second == -1 or next.first == -1)
      ans /= prev.first, ans *= next.second;
    else {
      if (prev.first * next.first >= prev.second * next.second)
        ans /= prev.second, ans *= next.first;
      else
        ans /= prev.first, ans *= next.second;
    }
  }

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  const int64_t mod = 1e9 + 7;
  vector<int64_t> a(n);
  vector<int64_t> b, c;
  vector<pair<int64_t, bool>> d(n);
  for (auto i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      b.push_back(a[i]);
    else
      c.push_back(abs(a[i]));
    d.push_back({abs(a[i]), (a[i] < 0)});
  }
  //    sort(b.begin(), b.end(), greater<>());
  sort(c.begin(), c.end());
  sort(d.begin(), d.end(), greater<>());

  int64_t ans = 1;
  if (k % 2 and (b.size() == 0)) { // 選ぶ個数が奇数かつ負の値しかない
    for (auto i = 0; i < k; i++)
      ans = (ans * c[i]) % mod;
    cout << mod - ans << endl;
    return 0;
  }
  //    reverse(c.begin(), c.end());

  bool minus = false;
  pair<int64_t, int64_t> prev, next;
  for (auto i = 0; i < k; i++) {
    ans = (ans * d[i].first) % mod;
    if (d[i].second)
      minus = not minus, prev.second = d[i].first;
    else
      prev.first = d[i].first;
  }

  if (minus) {
    auto cnt = bitset<2>(3);
    for (auto i = k; i < n; i++) {
      if (d[i].second and cnt[0])
        next.second = d[i].first, cnt[0] = false;
      else if (not d[i].second and cnt[1])
        next.first = d[i].first, cnt[1] = false;
      if (cnt == 0)
        break;
    }
    if (prev.second * next.first > prev.first * next.second) {
      ans = (ans / prev.second) % mod;
      ans = (ans * next.first) % mod;
    } else {
      ans = (ans / prev.first) % mod;
      ans = (ans * next.second) % mod;
    }
  }

  cout << ans << endl;
  return 0;
}