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

#include <bits/stdc++.h>

using ll = long long;
using namespace std;
const int INFint = 1e9 + 1;
const ll INFll = (ll)1e18 + 1;
template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

const int MOD = 1000000007;
using mint = Fp<MOD>;
int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> X, A, B;
  vector<pair<ll, ll>> Y;
  bool allminus(true);
  mint ans(1);
  for (int i(0); i < N; i++) {
    ll a;
    cin >> a;
    if (a >= 0) {
      A.push_back(a);
      allminus = false;
    } else
      B.push_back(abs(a));
    Y.push_back({abs(a), a});
  }
  if (allminus) {
    if (K % 2 == 0) {
      sort(B.rbegin(), B.rend());
    } else {
      sort(B.begin(), B.end());
      ans = -1;
    }
    for (int i(0); i < K; i++) {
      ans *= B[i];
    }
    cout << ans << endl;
    return 0;
  }
  if (N == K) {
    for (int i(0); i < N; i++) {
      ans *= Y[i].second;
    }
    cout << ans << endl;
    return 0;
  } else {
    int minp(1e9 + 1), minm(1e9 + 1);
    sort(Y.rbegin(), Y.rend());
    int mcnt(0);
    for (int i(0); i < K; i++) {
      ans *= Y[i].second;
      if (Y[i].second < 0)
        mcnt++;
      if (Y[i].second >= 0)
        minp = Y[i].second;
      else
        minm = Y[i].second;
    }
    ll min2p(1e9 + 1), min2m(1e9 + 1);
    for (int i(K); i < N; i++) {
      if (Y[i].second >= 0) {
        min2p = Y[i].second;
        break;
      }
    }
    for (int i(K); i < N; i++) {
      if (Y[i].second < 0) {
        min2m = Y[i].second;
        break;
      }
    }
    if (mcnt % 2 == 0) {
      cout << ans << endl;
      return 0;
    } else {
      if (minp == 0) {
        cout << 0 << endl;
        return 0;
      }
      // cout << ans << endl;
      ans = 1;
      bool used(false);
      // min2m/minp, min2p/minm
      // cout << min2m << " " << minm << " " << minp << " " << min2p << endl;
      if (((min2m * minm > min2p * minp) || min2p == 1e9 + 1) &&
          minp != 1e9 + 1) {
        for (int i(0); i < K; i++) {
          if (used == false && Y[i].second == minp) {
            used = true;
            continue;
          }
          ans *= Y[i].second;
        }
        ans *= min2m;
      } else {
        for (int i(0); i < K; i++) {
          if (used == false && Y[i].second == minm) {
            used = true;
            continue;
          }
          ans *= Y[i].second;
        }
        ans *= min2p;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using ll = long long;
using namespace std;
const int INFint = 1e9 + 1;
const ll INFll = (ll)1e18 + 1;
template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

const int MOD = 1000000007;
using mint = Fp<MOD>;
int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> X, A, B;
  vector<pair<ll, ll>> Y;
  bool allminus(true);
  mint ans(1);
  for (int i(0); i < N; i++) {
    ll a;
    cin >> a;
    if (a >= 0) {
      A.push_back(a);
      allminus = false;
    } else
      B.push_back(abs(a));
    Y.push_back({abs(a), a});
  }
  if (allminus) {
    if (K % 2 == 0) {
      sort(B.rbegin(), B.rend());
    } else {
      sort(B.begin(), B.end());
      ans = -1;
    }
    for (int i(0); i < K; i++) {
      ans *= B[i];
    }
    cout << ans << endl;
    return 0;
  }
  if (N == K) {
    for (int i(0); i < N; i++) {
      ans *= Y[i].second;
    }
    cout << ans << endl;
    return 0;
  } else {
    int minp(1e9 + 1), minm(1e9 + 1);
    sort(Y.rbegin(), Y.rend());
    int mcnt(0);
    for (int i(0); i < K; i++) {
      ans *= Y[i].second;
      if (Y[i].second < 0)
        mcnt++;
      if (Y[i].second >= 0)
        minp = Y[i].second;
      else
        minm = Y[i].second;
    }
    ll min2p(1e9 + 1), min2m(1e9 + 1);
    for (int i(K); i < N; i++) {
      if (min2p >= 0) {
        min2p = Y[i].second;
        break;
      }
    }
    for (int i(K); i < N; i++) {
      if (min2m < 0) {
        min2m = Y[i].second;
        break;
      }
    }
    if (mcnt % 2 == 0) {
      cout << ans << endl;
      return 0;
    } else {
      bool used(false);
      if (abs(min2m / minp) > abs(min2p / minm) || min2p == 1e9 + 1) {
        for (int i(0); i < K; i++) {
          if (used == false && Y[i].second == minp) {
            used = true;
            continue;
          }
          ans *= Y[i].second;
        }
        ans *= -min2m;
      } else {
        for (int i(0); i < K; i++) {
          if (used == false && Y[i].second == minm) {
            used = true;
            continue;
          }
          ans *= Y[i].second;
        }
        ans *= min2p;
      }
    }
  }
  cout << ans << endl;
  return 0;
}