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

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

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vvint = vector<vint>;
using vvvint = vector<vvint>;
using vdouble = vector<double>;
using vvdouble = vector<vdouble>;
using vvvdouble = vector<vvdouble>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using uint = unsigned int;
using ull = unsigned long long;

template <typename T> using uset = unordered_set<T>;
template <typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\\n';
constexpr int mod = 1000000007;
constexpr int mod2 = 998244353;
template <typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template <typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return (a > b && (a = b, true));
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return (a < b && (a = b, true));
}

template <typename T> vector<T> makeVector(size_t a, T b) {
  return (vector<T>(a, b));
}
template <typename... Ts> auto makeVector(size_t a, Ts... ts) {
  return (vector<decltype(makeVector(ts...))>(a, makeVector(ts...)));
}

template <typename T> bool isin(T y, T x, T h, T w) {
  return (0 <= y && 0 <= x && y < h && x < w);
}
template <typename T> bool isin1(T y, T x, T h, T w) {
  return (0 < y && 0 < x && y <= h && x <= w);
}

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  if (v.size()) {
    os << v[0];
    for (int i = 1; i < v.size(); i++)
      os << " " << v[i];
  }
  return (os);
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &u : v)
    is >> u;
  return (is);
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return (is >> p.first >> p.second);
}

template <unsigned MOD> struct Modint {
  using uint = unsigned;
  using u64 = uint64_t;
  using i64 = int64_t;

  Modint(const i64 dat = 0)
      : dat(dat >= 0 ? dat % MOD : (MOD - (-dat) % MOD) % MOD) {}

  constexpr Modint operator+(const Modint &v) const {
    return (Modint(*this) += v);
  }
  constexpr Modint operator-() const { return (Modint(-((i64)dat))); }
  constexpr Modint operator-(const Modint &v) const {
    return (Modint(*this) -= v);
  }
  constexpr Modint operator*(const Modint &v) const {
    return (Modint(*this) *= v);
  }

  constexpr Modint &operator=(const i64 &v) {
    return ((*this) = Modint<MOD>(v));
  }
  constexpr Modint &operator+=(const Modint &v) {
    dat += v.dat;
    if (dat >= MOD)
      dat -= MOD;
    return (*this);
  }
  constexpr Modint &operator-=(const Modint &v) {
    if (dat < v.dat)
      dat += MOD;
    dat -= v.dat;
    return (*this);
  }
  constexpr Modint &operator*=(const Modint &v) {
    dat = dat * v.dat % MOD;
    return (*this);
  }

  constexpr Modint operator~() const {
    i64 a = dat, b = MOD, x1 = 1, x2 = 0, t;

    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(x1 -= t * x2, x2);
    }

    return (Modint(x1));
  }

  constexpr Modint operator/(const Modint &v) const { return ((*this) * ~v); }
  constexpr Modint &operator/=(const Modint &v) {
    return ((*this) = (*this) / v);
  }

  constexpr Modint operator^(u64 n) const {
    Modint<MOD> ret(1), mul(dat);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return (ret);
  }

  bool operator==(const Modint &v) { return (dat == v.dat); }
  bool operator!=(const Modint &v) { return (dat != v.dat); }

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

  friend istream &operator>>(istream &is, Modint &p) {
    i64 data;
    is >> data;
    p = Modint<MOD>(data);
    return (is);
  }

private:
  u64 dat;
};

void Main() {
  ll N, K;
  cin >> N >> K;
  vll A(N);
  cin >> A;
  sort(begin(A), end(A), [](ll a, ll b) { return (abs(a) < abs(b)); });

  bool plus = false;
  for (int i = 0; i < N; i++) {
    if (A[i] >= 0)
      plus = true;
  }
  if (!plus && (K & 1)) {
    Modint<mod> ans = 1;
    for (int i = 0; i < K; i++) {
      ans *= A[i];
    }
    cout << ans << endl;
    return;
  }

  int cnt = 0;
  for (int i = N - 1; i >= N - K; i--) {
    if (A[i] < 0)
      cnt++;
  }

  auto calc = [&](int cn) {
    int p = K - cn, m = cn;
    Modint<mod> ans = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (A[i] >= 0 && p > 0) {
        p--;
        ans *= A[i];
      }
      if (A[i] < 0 && m > 0) {
        m--;
        ans *= A[i];
      }
    }
    return (ans);
  };

  if (cnt % 2 == 0) {
    cout << calc(cnt) << endl;
  } else {
    ll val1 = 1, val2 = 1;
    ll pcnt = 0, mcnt = 0;
    if (cnt == K) {
      cout << calc(cnt - 1) << endl;
      return;
    }
    for (int i = N - 1; i >= 0; i--) {
      if (A[i] >= 0)
        pcnt++;
      else
        mcnt++;
      if (A[i] >= 0 && (pcnt == K - cnt || pcnt == K - cnt + 1))
        val1 *= A[i];
      if (A[i] < 0 && (mcnt == cnt || mcnt == cnt + 1))
        val2 *= A[i];
    }

    if (pcnt < K - cnt + 1 && mcnt < cnt + 1) {
      Modint<mod> ans = 1;
      for (int i = 0; i < K; i++) {
        ans *= A[i];
      }
      cout << ans << endl;
    } else if (pcnt < K - cnt + 1) {
      cout << calc(cnt + 1) << endl;
    } else if (mcnt < cnt + 1) {
      cout << calc(cnt - 1) << endl;
    } else if (val1 > val2) {
      cout << calc(cnt - 1) << endl;
    } else {
      cout << calc(cnt + 1) << endl;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
  Main();
  return (0);
}


Error: 0

Fixed submission:

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

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vvint = vector<vint>;
using vvvint = vector<vvint>;
using vdouble = vector<double>;
using vvdouble = vector<vdouble>;
using vvvdouble = vector<vvdouble>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using uint = unsigned int;
using ull = unsigned long long;

template <typename T> using uset = unordered_set<T>;
template <typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\\n';
constexpr int mod = 1000000007;
constexpr int mod2 = 998244353;
template <typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template <typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return (a > b && (a = b, true));
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return (a < b && (a = b, true));
}

template <typename T> vector<T> makeVector(size_t a, T b) {
  return (vector<T>(a, b));
}
template <typename... Ts> auto makeVector(size_t a, Ts... ts) {
  return (vector<decltype(makeVector(ts...))>(a, makeVector(ts...)));
}

template <typename T> bool isin(T y, T x, T h, T w) {
  return (0 <= y && 0 <= x && y < h && x < w);
}
template <typename T> bool isin1(T y, T x, T h, T w) {
  return (0 < y && 0 < x && y <= h && x <= w);
}

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  if (v.size()) {
    os << v[0];
    for (int i = 1; i < v.size(); i++)
      os << " " << v[i];
  }
  return (os);
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &u : v)
    is >> u;
  return (is);
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return (is >> p.first >> p.second);
}

template <unsigned MOD> struct Modint {
  using uint = unsigned;
  using u64 = uint64_t;
  using i64 = int64_t;

  Modint(const i64 dat = 0)
      : dat(dat >= 0 ? dat % MOD : (MOD - (-dat) % MOD) % MOD) {}

  constexpr Modint operator+(const Modint &v) const {
    return (Modint(*this) += v);
  }
  constexpr Modint operator-() const { return (Modint(-((i64)dat))); }
  constexpr Modint operator-(const Modint &v) const {
    return (Modint(*this) -= v);
  }
  constexpr Modint operator*(const Modint &v) const {
    return (Modint(*this) *= v);
  }

  constexpr Modint &operator=(const i64 &v) {
    return ((*this) = Modint<MOD>(v));
  }
  constexpr Modint &operator+=(const Modint &v) {
    dat += v.dat;
    if (dat >= MOD)
      dat -= MOD;
    return (*this);
  }
  constexpr Modint &operator-=(const Modint &v) {
    if (dat < v.dat)
      dat += MOD;
    dat -= v.dat;
    return (*this);
  }
  constexpr Modint &operator*=(const Modint &v) {
    dat = dat * v.dat % MOD;
    return (*this);
  }

  constexpr Modint operator~() const {
    i64 a = dat, b = MOD, x1 = 1, x2 = 0, t;

    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(x1 -= t * x2, x2);
    }

    return (Modint(x1));
  }

  constexpr Modint operator/(const Modint &v) const { return ((*this) * ~v); }
  constexpr Modint &operator/=(const Modint &v) {
    return ((*this) = (*this) / v);
  }

  constexpr Modint operator^(u64 n) const {
    Modint<MOD> ret(1), mul(dat);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return (ret);
  }

  bool operator==(const Modint &v) { return (dat == v.dat); }
  bool operator!=(const Modint &v) { return (dat != v.dat); }

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

  friend istream &operator>>(istream &is, Modint &p) {
    i64 data;
    is >> data;
    p = Modint<MOD>(data);
    return (is);
  }

private:
  u64 dat;
};

void Main() {
  ll N, K;
  cin >> N >> K;
  vll A(N);
  cin >> A;
  sort(begin(A), end(A), [](ll a, ll b) { return (abs(a) < abs(b)); });

  ll sign = 0;
  Modint<mod> ans = 1;
  for (int i = N - 1; i >= N - K; i--) {
    if (A[i] < 0)
      sign = 1 - sign;
    else if (A[i] == 0) {
      cout << 0 << endl;
      return;
    }
    ans *= (mod + A[i]);
  }
  if (sign == 0) {
    cout << ans << endl;
    return;
  }
  abort();
  for (int i = N - K; i < N; i++) {
    if (A[i] < 0) {
      ans /= A[i];
      break;
    }
  }

  bool f = false;
  for (int i = N - K - 1; i >= 0; i--) {
    if (A[i] >= 0) {
      ans *= A[i];
      f = true;
      break;
    }
  }
  if (f) {
    cout << ans << endl;
    return;
  }

  ans = 1;
  for (int i = 0; i < K; i++) {
    ans *= (mod + A[i]);
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
  Main();
  return (0);
}