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

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

struct ModInt {
  using M = ModInt;

  static vector<M> fact, finv;
  static long long MOD;
  long long v;

  // normalize: [-MOD, MOD**2] -> [0, MOD)
  ModInt(const long long _v = 0) : v((_v < 0) ? _v % MOD + MOD : _v % MOD) {}

  M operator+(const M &x) const { return M(v + x.v); }
  M operator-(const M &x) const { return M(v - x.v); }
  M operator*(const M &x) const { return M(v * x.v); }
  M operator/(const M &x) const { return M(v * x.inv().v); }
  M &operator+=(const M &x) { return *this = *this + x; }
  M &operator-=(const M &x) { return *this = *this - x; }
  M &operator*=(const M &x) { return *this = *this * x; }
  M &operator/=(const M &x) { return *this = *this / x; }
  bool operator==(const M &x) const { return v == x.v; }
  bool operator!=(const M &x) const { return v != x.v; }
  friend istream &operator>>(istream &input, M &x) {
    return input >> x.v, x = M(x), input;
  }
  friend ostream &operator<<(ostream &output, const M &x) {
    return output << x.v;
  }
  inline M pow(long long n) const {
    M x(v), res(1);
    while (n) {
      if (n & 1)
        res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  inline M inv() const { return this->pow(MOD - 2); }
  static void build(int n) {
    fact.assign(n + 1, 1);
    for (int i = 1; i < n + 1; i++)
      fact[i] = fact[i - 1] * M(i);
    finv.assign(n + 1, fact[n].inv());
    for (int i = n; i > 0; i--)
      finv[i - 1] = finv[i] * M(i);
  }
  static M comb(int n, int k) {
    if (n < k || k < 0)
      return M(0);
    return fact[n] * finv[n - k] * finv[k];
  }
  static M extgcd(int a, int b, int &x, int &y) {
    M d(a);
    if (b) {
      d = extgcd(b, a % b, y, x);
      y -= (a / b) * x;
    } else {
      x = 1;
      y = 0;
    }
    return d;
  }
};
vector<ModInt> ModInt::fact = vector<ModInt>();
vector<ModInt> ModInt::finv = vector<ModInt>();
long long ModInt::MOD = 1e9 + 7;

using ll = long long;
const ll INF = 2e9;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  bool all_neg = all_of(begin(a), end(a), [](int x) { return x < 0; });
  if (all_neg && k % 2 == 1) {
    ModInt ans = 1;
    sort(begin(a), end(a), [](int x, int y) { return abs(x) < abs(y); });
    for (int i = 0; i < k; i++)
      ans *= a[i];
    cout << ans << endl;
    return 0;
  }

  sort(rbegin(a), rend(a), [](int x, int y) { return abs(x) < abs(y); });
  ModInt ans = 1;
  int c = 0;
  ll mnp = INF, mnm = INF;
  for (int i = 0; i < k; i++) {
    ans *= a[i];
    ll b = abs(a[i]);
    if (a[i] < 0) {
      c++;
      mnm = min(mnm, b);
    } else {
      mnp = min(mnp, b);
    }
  }
  ll mxp = -1, mxm = -1;
  for (int i = k; i < n; i++) {
    ll b = abs(a[i]);
    if (a[i] < 0) {
      mxm = max(mxm, b);
    } else {
      mxp = max(mxp, b);
    }
  }

  if (c % 2 == 1) {
    if (mnp < INF && mnm < INF && mxp > -1 && mxm > -1) {
      ans = (mnm * mxm < mnp * mxp) ? ans / -mnm * mxp : ans / mnp * -mxm;
    } else if (mnm < INF && mxp > -1) {
      ans = ans / -mnm * mxp;
    } else if (mnp < INF && mxm > -1) {
      ans = ans / mnp * -mxm;
    }
  }

  cout << ans << endl;
}

Error: WA

Fixed submission:

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

struct ModInt {
  using M = ModInt;

  static vector<M> fact, finv;
  static long long MOD;
  long long v;

  // normalize: [-MOD, MOD**2] -> [0, MOD)
  ModInt(const long long _v = 0) : v((_v < 0) ? _v % MOD + MOD : _v % MOD) {}

  M operator+(const M &x) const { return M(v + x.v); }
  M operator-(const M &x) const { return M(v - x.v); }
  M operator*(const M &x) const { return M(v * x.v); }
  M operator/(const M &x) const { return M(v * x.inv().v); }
  M &operator+=(const M &x) { return *this = *this + x; }
  M &operator-=(const M &x) { return *this = *this - x; }
  M &operator*=(const M &x) { return *this = *this * x; }
  M &operator/=(const M &x) { return *this = *this / x; }
  bool operator==(const M &x) const { return v == x.v; }
  bool operator!=(const M &x) const { return v != x.v; }
  friend istream &operator>>(istream &input, M &x) {
    return input >> x.v, x = M(x), input;
  }
  friend ostream &operator<<(ostream &output, const M &x) {
    return output << x.v;
  }
  inline M pow(long long n) const {
    M x(v), res(1);
    while (n) {
      if (n & 1)
        res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  inline M inv() const { return this->pow(MOD - 2); }
  static void build(int n) {
    fact.assign(n + 1, 1);
    for (int i = 1; i < n + 1; i++)
      fact[i] = fact[i - 1] * M(i);
    finv.assign(n + 1, fact[n].inv());
    for (int i = n; i > 0; i--)
      finv[i - 1] = finv[i] * M(i);
  }
  static M comb(int n, int k) {
    if (n < k || k < 0)
      return M(0);
    return fact[n] * finv[n - k] * finv[k];
  }
  static M extgcd(int a, int b, int &x, int &y) {
    M d(a);
    if (b) {
      d = extgcd(b, a % b, y, x);
      y -= (a / b) * x;
    } else {
      x = 1;
      y = 0;
    }
    return d;
  }
};
vector<ModInt> ModInt::fact = vector<ModInt>();
vector<ModInt> ModInt::finv = vector<ModInt>();
long long ModInt::MOD = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> p, m;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a < 0) {
      m.push_back(-a);
    } else {
      p.push_back(a);
    }
  }
  if (m.size() == 0) {
    sort(p.rbegin(), p.rend());
    ModInt ans = 1;
    for (int i = 0; i < k; i++) {
      ans *= ModInt(p[i]);
    }
    cout << ans << endl;
    return 0;
  }
  if (p.size() == 0 && k % 2 == 1) {
    sort(m.begin(), m.end());
    ModInt ans = -1;
    for (int i = 0; i < k; i++) {
      ans *= ModInt(m[i]);
    }
    cout << ans << endl;
    return 0;
  }
  if (p.size() == 0 && k % 2 == 0) {
    sort(m.rbegin(), m.rend());
    ModInt ans = 1;
    for (int i = 0; i < k; i++) {
      ans *= ModInt(m[i]);
    }
    cout << ans << endl;
    return 0;
  }
  sort(p.rbegin(), p.rend());
  sort(m.rbegin(), m.rend());
  int mx = 0;
  long double tmp = 1.0L;
  for (int i = 2; i <= m.size(); i++) {
    int j = k - i;
    if (j < 0)
      continue;
    tmp *= 1.0L * m[i - 2] / p[j + 1] * m[i - 1] / p[j];
    cout << tmp << endl;
    if (tmp > 1) {
      mx = i;
      tmp = 1.0L;
    }
  }
  ModInt ans = 1;
  for (int i = 0; i < mx; i++) {
    ans *= ModInt(m[i]);
  }
  for (int i = 0; i < k - mx; i++) {
    ans *= ModInt(p[i]);
  }
  cout << ans << endl;
}