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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1000000007;

struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct cominit {
  vector<mint> fact, ifact;
  cominit(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; i--)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int r) {
    if (n < r or r < 0)
      return 0;
    return fact[n] * ifact[r] * ifact[n - r];
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int &i : a)
    cin >> i;
  mint ans = 1;
  if (n == k) {
    for (int i = 0; i < n; i++)
      ans *= a[i];
    cout << ans << endl;
    return 0;
  }
  vector<int> s, t;
  for (int i = 0; i < n; i++) {
    if (a[i] > 0)
      s.push_back(a[i]);
    else
      t.push_back(a[i]);
  }
  if (s.size() == 0 && k % 2 == 1) {
    sort(a.rbegin(), a.rend());
    for (int i = 0; i < k; i++)
      ans *= a[i];
    cout << ans << endl;
    return 0;
  }
  sort(s.begin(), s.end());
  sort(t.rbegin(), t.rend());
  if (k % 2 == 1) {
    ans *= s.back();
    s.pop_back();
    k--;
  }
  vector<ll> mul;
  while (s.size() >= 2) {
    ll x = s.back();
    s.pop_back();
    x *= s.back();
    s.pop_back();
    mul.push_back(x);
  }
  while (t.size() >= 2) {
    ll x = t.back();
    t.pop_back();
    x *= t.back();
    t.pop_back();
    mul.push_back(x);
  }
  sort(mul.rbegin(), mul.rend());
  for (int i = 0; i < k / 2; i++)
    ans *= mul[i];
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1000000007;

struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct cominit {
  vector<mint> fact, ifact;
  cominit(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; i--)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int r) {
    if (n < r or r < 0)
      return 0;
    return fact[n] * ifact[r] * ifact[n - r];
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int &i : a)
    cin >> i;
  mint ans = 1;
  if (n == k) {
    for (int i = 0; i < n; i++)
      ans *= a[i];
    cout << ans << endl;
    return 0;
  }
  vector<int> s, t;
  for (int i = 0; i < n; i++) {
    if (a[i] > 0)
      s.push_back(a[i]);
    else
      t.push_back(a[i]);
  }
  if (s.size() == 0 && k % 2 == 1) {
    sort(a.rbegin(), a.rend());
    for (int i = 0; i < k; i++)
      ans *= a[i];
    cout << ans << endl;
    return 0;
  }
  sort(s.begin(), s.end());
  sort(t.rbegin(), t.rend());
  if (k % 2 == 1) {
    ans *= s.back();
    s.pop_back();
  }
  vector<ll> mul;
  while (s.size() >= 2) {
    ll x = s.back();
    s.pop_back();
    x *= s.back();
    s.pop_back();
    mul.push_back(x);
  }
  while (t.size() >= 2) {
    ll x = t.back();
    t.pop_back();
    x *= t.back();
    t.pop_back();
    mul.push_back(x);
  }
  sort(mul.rbegin(), mul.rend());
  for (int i = 0; i < k; i++)
    ans *= mul[i];
  cout << ans << endl;
  return 0;
}