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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
ll dx[4] = {0, 1, -1, 0};
ll dy[4] = {1, 0, 0, -1};
template <int mod = 1000000007> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

const int mod = 1000000007;
// const int mod = 998244353;
using mint = ModInt<mod>;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int n, k;
  cin >> n >> k;
  vector<ll> v;
  int z = 0;
  int m = 0;
  ll a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (a != 0)
      v.push_back(a);
    if (a == 0)
      z++;
    if (a < 0)
      m++;
  }
  if (z > n - k) {
    cout << 0 << endl;
    return 0;
  }
  if (n == m + z) {
    mint ans = 1;
    if (k % 2) {
      sort(v.begin(), v.end(), greater<ll>());
      for (int i = 0; i < k; i++) {
        ans *= v[i];
      }
      if (z)
        ans = 0;
    } else {
      sort(v.begin(), v.end());
      for (int i = 0; i < k; i++) {
        ans *= v[i];
      }
    }
    cout << ans << endl;
    return 0;
  }
  mint ans = 1;
  sort(v.begin(), v.end(), greater<ll>());
  for (int i = 0; i < k; i++)
    ans = ans * v[i];
  int r = k - 1;
  int now = v.size() - 1;
  while (r >= 1 && v[now - 1] < 0) {
    ll p = v[r] * v[r - 1];
    ll m = v[now] * v[now - 1];
    if (p <= m) {
      ans /= p;
      ans *= m;
      r -= 2;
      now -= 2;
    } else
      break;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
ll dx[4] = {0, 1, -1, 0};
ll dy[4] = {1, 0, 0, -1};
long double eps = 1e-6;
long double pi = acos(-1);

ll mod = 1e9 + 7;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  ll n, k;
  cin >> n >> k;
  if (n == k) {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      ll a;
      cin >> a;
      ans *= a;
      ans %= mod;
    }
    if (ans < 0)
      ans = mod - ans;
    cout << ans << endl;
    return 0;
  }
  array<ll, 2> a[n];
  ll mini = 0;
  for (int i = 0; i < n; i++) {
    ll t;
    cin >> t;
    if (t < 0) {
      a[i] = {-t, 1};
      mini++;
    } else
      a[i] = {t, 0};
  }
  if (mini == n) {
    ll ans = 1;
    sort(a, a + n);
    for (int i = 0; i < k; i++)
      ans = (ans * a[i][0]) % mod;
    ans = mod - ans;
    ans %= mod;
    cout << ans << endl;
    return 0;
  }
  sort(a, a + n, greater<array<ll, 2>>());

  ll dp[n + 1][2];
  dp[0][1] = 1;
  dp[0][0] = 1;
  int now = 0;
  int sum = 0;
  bool ok = false;
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    if (a[i][1] == 1) {
      now ^= 1;
      if (now) {
        dp[i + 1][0] = dp[i][0];
        dp[i + 1][1] = (dp[i][0] * a[i][0]) % mod;
      } else {
        if (i + 1 <= k) {
          dp[i + 1][0] = (dp[i][1] * a[i][0]) % mod;
          dp[i + 1][1] = dp[i + 1][0];
          sum = i + 1;
        } else {
          dp[i + 1][0] = dp[i][0];
        }
      }
    } else {
      dp[i + 1][1] = (dp[i][1] * a[i][0]) % mod;
      dp[i + 1][0] = (dp[i][0] * a[i][0]);
      sum++;
      dp[i + 1][0] %= mod;
    }
    if (sum == k) {
      ok = 1;
      ans = dp[i + 1][0];
      break;
    }
  }
  assert(ok);

  cout << ans << endl;
}