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

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

static const long long mod = 1000000007;
struct mint {
  long long x;
  mint(ll x = 0) : x(x % mod) {}
  mint &operator+=(const mint a) {
    (x += a.x) %= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    (x += mod - a.x) %= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint ret(*this);
    return ret += a;
  }
  mint operator-(const mint a) const {
    mint ret(*this);
    return ret -= a;
  }
  mint operator*(const mint a) const {
    mint ret(*this);
    return ret *= a;
  }
  mint pow(ll t) const {
    if (t == 0)
      return mint(1);
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint ret(*this);
    return ret /= a;
  }
};
ostream &operator<<(ostream &os, const mint &x) {
  os << x.x;
  return os;
}
class modutils {
  vector<mint> fact, invfact;

public:
  modutils(int n = 2000005) : fact(n + 1), invfact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * i;
    invfact[n] = fact[n].inv();
    for (int i = n; i >= 1; i--)
      invfact[i - 1] = invfact[i] * i;
  }
  mint pow(mint x, ll n) { return x.pow(n); }
  mint comb(ll n, ll k) {
    if (n < 0 || k < 0 || n < k)
      return 0;
    return fact[n] * invfact[k] * invfact[n - k];
  }
  mint perm(ll n, ll k) {
    if (n < 0 || k < 0 || n < k)
      return 0;
    return fact[n] * invfact[n - k];
  }
  mint fac(ll n) { return fact[n]; }
};
modutils mutil;

struct ST {
  ll eval;
  bool flg;
  vector<ll> v;
};
bool operator<(const ST &a, const ST &b) {
  if (a.eval == b.eval)
    return a.v.size() > b.v.size();
  return a.eval > b.eval;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> v_n, v_z, v_p;
  rep(i, N) {
    ll a;
    cin >> a;

    if (a < 0)
      v_n.push_back(a);
    if (a == 0)
      v_z.push_back(a);
    if (a > 0)
      v_p.push_back(a);
  }

  if (v_z.size() > 0) {
    if (v_n.size() + v_p.size() < K) {
      cout << 0 << endl;
      return 0;
    }
  }
  sort(ALLOF(v_p));
  reverse(ALLOF(v_p));
  sort(ALLOF(v_n));

  if (K == 1) {
    if (v_p.size() > 0) {
      cout << v_p[0] << endl;
    } else if (v_z.size() > 0) {
      cout << 0 << endl;
    } else {
      cout << v_n[v_n.size() - 1] << endl;
    }
    return 0;
  }

  vector<ST> v;
  for (int i = 0; i < v_n.size(); i += 2) {
    if (i + 1 >= v_n.size())
      continue;
    ST tmp;
    tmp.eval = v_n[i] * v_n[i + 1];
    tmp.flg = true;
    tmp.v.push_back(-v_n[i]);
    tmp.v.push_back(-v_n[i + 1]);
    v.push_back(tmp);
  }
  sort(ALLOF(v));
  mint ret(1);
  int k = 0;

  if (K % 2 == 1 && v_p.size() > 0) {
    ret *= v_p[0];
    k++;
    for (int i = 1; i < v_p.size(); i += 2) {
      if (i + 1 < v_p.size()) {
        ST tmp;
        tmp.eval = v_p[i] * v_p[i + 1];
        tmp.flg = false;
        tmp.v.push_back(v_p[i]);
        tmp.v.push_back(v_p[i + 1]);
        v.push_back(tmp);
      } else {
        ST tmp;
        tmp.eval = v_p[i];
        tmp.flg = false;
        tmp.v.push_back(v_p[i]);
        v.push_back(tmp);
      }
    }
  } else {
    for (int i = 0; i < v_p.size(); i += 2) {
      if (i + 1 < v_p.size()) {
        ST tmp;
        tmp.eval = v_p[i] * v_p[i + 1];
        tmp.flg = false;
        tmp.v.push_back(v_p[i]);
        tmp.v.push_back(v_p[i + 1]);
        v.push_back(tmp);
      } else {
        ST tmp;
        tmp.eval = v_p[i];
        tmp.flg = false;
        tmp.v.push_back(v_p[i]);
        v.push_back(tmp);
      }
    }
  }

  sort(ALLOF(v));
  rep(i, v.size()) {
    if (k + v[i].v.size() <= K) {
      ret *= v[i].eval;
      k += v[i].v.size();
    }
  }

  if (k != K) {
    if (v_z.size() > 0) {
      cout << 0 << endl;
    } else {
      vector<ll> v;
      rep(i, v_n.size()) { v.push_back(-v_n[i]); }
      rep(i, v_p.size()) { v.push_back(v_p[i]); }
      sort(ALLOF(v));
      mint tmp(1);
      tmp *= mod - 1;
      rep(i, K) { tmp *= v[i]; }
      cout << tmp << endl;
    }
    return 0;
  }

  cout << ret << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

static const long long mod = 1000000007;
struct mint {
  long long x;
  mint(ll x = 0) : x(x % mod) {}
  mint &operator+=(const mint a) {
    (x += a.x) %= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    (x += mod - a.x) %= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint ret(*this);
    return ret += a;
  }
  mint operator-(const mint a) const {
    mint ret(*this);
    return ret -= a;
  }
  mint operator*(const mint a) const {
    mint ret(*this);
    return ret *= a;
  }
  mint pow(ll t) const {
    if (t == 0)
      return mint(1);
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint ret(*this);
    return ret /= a;
  }
};
ostream &operator<<(ostream &os, const mint &x) {
  os << x.x;
  return os;
}
class modutils {
  vector<mint> fact, invfact;

public:
  modutils(int n = 2000005) : fact(n + 1), invfact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * i;
    invfact[n] = fact[n].inv();
    for (int i = n; i >= 1; i--)
      invfact[i - 1] = invfact[i] * i;
  }
  mint pow(mint x, ll n) { return x.pow(n); }
  mint comb(ll n, ll k) {
    if (n < 0 || k < 0 || n < k)
      return 0;
    return fact[n] * invfact[k] * invfact[n - k];
  }
  mint perm(ll n, ll k) {
    if (n < 0 || k < 0 || n < k)
      return 0;
    return fact[n] * invfact[n - k];
  }
  mint fac(ll n) { return fact[n]; }
};
modutils mutil;

struct ST {
  ll eval;
  bool flg;
  vector<ll> v;
};
bool operator<(const ST &a, const ST &b) {
  if (a.eval == b.eval)
    return a.v.size() > b.v.size();
  return a.eval > b.eval;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> v_n, v_z, v_p;
  rep(i, N) {
    ll a;
    cin >> a;

    if (a < 0)
      v_n.push_back(a);
    if (a == 0)
      v_z.push_back(a);
    if (a > 0)
      v_p.push_back(a);
  }

  if (v_z.size() > 0) {
    if (v_n.size() + v_p.size() < K) {
      cout << 0 << endl;
      return 0;
    }
  }
  sort(ALLOF(v_p));
  reverse(ALLOF(v_p));
  sort(ALLOF(v_n));

  if (K == 1) {
    if (v_p.size() > 0) {
      cout << v_p[0] << endl;
    } else if (v_z.size() > 0) {
      cout << 0 << endl;
    } else {
      cout << v_n[v_n.size() - 1] << endl;
    }
    return 0;
  }

  vector<ST> v;
  for (int i = 0; i < v_n.size(); i += 2) {
    if (i + 1 >= v_n.size())
      continue;
    ST tmp;
    tmp.eval = v_n[i] * v_n[i + 1];
    tmp.flg = true;
    tmp.v.push_back(-v_n[i]);
    tmp.v.push_back(-v_n[i + 1]);
    v.push_back(tmp);
  }
  sort(ALLOF(v));
  mint ret(1);
  int k = 0;

  if (K % 2 == 1) {
    ret *= v_p[0];
    k++;
    for (int i = 1; i < v_p.size(); i += 2) {
      if (i + 1 < v_p.size()) {
        ST tmp;
        tmp.eval = v_p[i] * v_p[i + 1];
        tmp.flg = false;
        tmp.v.push_back(v_p[i]);
        tmp.v.push_back(v_p[i + 1]);
        v.push_back(tmp);
      } else {
        ST tmp;
        tmp.eval = v_p[i];
        tmp.flg = false;
        tmp.v.push_back(v_p[i]);
        v.push_back(tmp);
      }
    }
  } else {
    for (int i = 0; i < v_p.size(); i += 2) {
      if (i + 1 < v_p.size()) {
        ST tmp;
        tmp.eval = v_p[i] * v_p[i + 1];
        tmp.flg = false;
        tmp.v.push_back(v_p[i]);
        tmp.v.push_back(v_p[i + 1]);
        v.push_back(tmp);
      } else {
        ST tmp;
        tmp.eval = v_p[i];
        tmp.flg = false;
        tmp.v.push_back(v_p[i]);
        v.push_back(tmp);
      }
    }
  }

  sort(ALLOF(v));
  rep(i, v.size()) {
    if (k + v[i].v.size() <= K) {
      ret *= v[i].eval;
      k += v[i].v.size();
    }
  }

  if (k != K) {
    if (v_z.size() > 0) {
      cout << 0 << endl;
    } else {
      vector<ll> v;
      rep(i, v_n.size()) { v.push_back(-v_n[i]); }
      rep(i, v_p.size()) { v.push_back(v_p[i]); }
      sort(ALLOF(v));
      mint tmp(1);
      tmp *= mod - 1;
      rep(i, K) { tmp *= v[i]; }
      cout << tmp << endl;
    }
    return 0;
  }

  cout << ret << endl;

  return 0;
}