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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long // [9,223,372,036,854,775,807 to -9.....808] 19 digits
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define mii map<int, int>
#define x first
#define y second
#define mem(a, x) memset(a, x, sizeof(a))
#define rep(i, a, b) for (int i = a; i < b; i++)
#define sz(x) (int)((x).size())

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

// const int32_t MOD=163577857; const int32_t hellhsh = 444440583533; const
// double pi=2*acos(0.0); const int32_t MOD=998244353;
const int32_t MOD = 1e9 + 7;
const int32_t maxn = 2e5 + 5;

struct mod_int {
  int32_t val;
  mod_int(long long v = 0) {
    if (v < 0)
      v = v % MOD + MOD;
    if (v >= MOD)
      v %= MOD;
    val = v;
  }

  static int32_t mod_inv(int32_t a, int32_t m = MOD) {
    int32_t g = m, r = a, x = 0, y = 1;
    while (r != 0) {
      int32_t q = g / r;
      g %= r;
      swap(g, r);
      x -= q * y;
      swap(x, y);
    }
    return x < 0 ? x + m : x;
  }
  static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
#if !defined(_WIN32) || defined(_WIN64)
    return x % m;
#endif
    unsigned x_high = x >> 32, x_low = (unsigned)x;
    unsigned quot, rem;
    asm("divl %4\\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
    return rem;
  }

  explicit operator int() const { return val; }
  mod_int &operator+=(const mod_int &other) {
    val += other.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  mod_int &operator-=(const mod_int &other) {
    val -= other.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  mod_int &operator*=(const mod_int &other) {
    val = fast_mod((uint64_t)val * other.val);
    return *this;
  }
  mod_int &operator/=(const mod_int &other) { return *this *= other.inv(); }
  friend mod_int operator+(const mod_int &a, const mod_int &b) {
    return mod_int(a) += b;
  }
  friend mod_int operator-(const mod_int &a, const mod_int &b) {
    return mod_int(a) -= b;
  }
  friend mod_int operator*(const mod_int &a, const mod_int &b) {
    return mod_int(a) *= b;
  }
  friend mod_int operator/(const mod_int &a, const mod_int &b) {
    return mod_int(a) /= b;
  }
  mod_int &operator++() {
    val = val == MOD - 1 ? 0 : val + 1;
    return *this;
  }
  mod_int &operator--() {
    val = val == 0 ? MOD - 1 : val - 1;
    return *this;
  }
  mod_int operator++(int32_t) {
    mod_int before = *this;
    ++*this;
    return before;
  }
  mod_int operator--(int32_t) {
    mod_int before = *this;
    --*this;
    return before;
  }
  mod_int operator-() const { return val == 0 ? 0 : MOD - val; }
  bool operator==(const mod_int &other) const { return val == other.val; }
  bool operator!=(const mod_int &other) const { return val != other.val; }
  mod_int inv() const { return mod_inv(val); }
  friend ostream &operator<<(ostream &stream, const mod_int &m) {
    return stream << m.val;
  }
  friend istream &operator>>(istream &stream, mod_int &m) {
    return stream >> m.val;
  }

  mod_int pow(long long p) const { // dont put a negetive p
    assert(p >= 0);
    mod_int a = *this, result = 1;
    while (p > 0) {
      if (p & 1)
        result *= a;

      a *= a;
      p >>= 1;
    }
    return result;
  }
};
mod_int fact[maxn];
bool fbb = 0;
void build() {
  fbb = 1;
  fact[0] = 1;
  rep(i, 1, maxn) fact[i] = i * fact[i - 1];
}
mod_int C(int n, int r) {
  if (fbb)
    return (fact[n] / (fact[r] * fact[n - r]));
  mod_int a = 1, b = 1;
  rep(i, 0, r) {
    a *= n - i;
    b *= i + 1;
  }
  return a / b;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vi pos, neg;
  rep(i, 0, n) {
    int x;
    cin >> x;
    if (x < 0)
      neg.pb(-x);
    else
      pos.pb(x);
  }
  sort(all(neg));
  sort(all(pos));
  reverse(all(neg));
  reverse(all(pos));
  mod_int ans = 1;
  if (n == k) {
    for (int i : pos)
      ans *= i;
    for (int i : neg)
      ans *= -i;
    cout << ans;
    return;
  }
  if (!sz(pos)) {
    if (k & 1)
      reverse(all(neg));
    rep(i, 0, k) ans *= -neg[i];
    cout << ans;
    return;
  }
  int x = k, i = 1, j = 0;
  for (i = 1; i < sz(neg) and k > 1; i += 2, k -= 2)
    ans *= neg[i] * neg[i - 1];
  while (k)
    ans *= pos[j++], k--;
  i -= 2;
  while (i > 0 and j < sz(pos) - 1) {
    if (neg[i] * neg[i - 1] < pos[j] * pos[j + 1]) {
      ans *= pos[j] * pos[j + 1];
      ans /= neg[i] * neg[i - 1];
      j += 2;
      i -= 2;
    } else
      break;
  }
  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tc = 1, k = 1;
  // cin>>tc;
  while (tc--) {
    // cout<<"Case #"<<k++<<": ";
    solve();
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long // [9,223,372,036,854,775,807 to -9.....808] 19 digits
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define mii map<int, int>
#define x first
#define y second
#define mem(a, x) memset(a, x, sizeof(a))
#define rep(i, a, b) for (int i = a; i < b; i++)
#define sz(x) (int)((x).size())

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

// const int32_t MOD=163577857; const int32_t hellhsh = 444440583533; const
// double pi=2*acos(0.0); const int32_t MOD=998244353;
const int32_t MOD = 1e9 + 7;
const int32_t maxn = 2e5 + 5;

struct mod_int {
  int32_t val;
  mod_int(long long v = 0) {
    if (v < 0)
      v = v % MOD + MOD;
    if (v >= MOD)
      v %= MOD;
    val = v;
  }

  static int32_t mod_inv(int32_t a, int32_t m = MOD) {
    int32_t g = m, r = a, x = 0, y = 1;
    while (r != 0) {
      int32_t q = g / r;
      g %= r;
      swap(g, r);
      x -= q * y;
      swap(x, y);
    }
    return x < 0 ? x + m : x;
  }
  static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
#if !defined(_WIN32) || defined(_WIN64)
    return x % m;
#endif
    unsigned x_high = x >> 32, x_low = (unsigned)x;
    unsigned quot, rem;
    asm("divl %4\\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
    return rem;
  }

  explicit operator int() const { return val; }
  mod_int &operator+=(const mod_int &other) {
    val += other.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  mod_int &operator-=(const mod_int &other) {
    val -= other.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  mod_int &operator*=(const mod_int &other) {
    val = fast_mod((uint64_t)val * other.val);
    return *this;
  }
  mod_int &operator/=(const mod_int &other) { return *this *= other.inv(); }
  friend mod_int operator+(const mod_int &a, const mod_int &b) {
    return mod_int(a) += b;
  }
  friend mod_int operator-(const mod_int &a, const mod_int &b) {
    return mod_int(a) -= b;
  }
  friend mod_int operator*(const mod_int &a, const mod_int &b) {
    return mod_int(a) *= b;
  }
  friend mod_int operator/(const mod_int &a, const mod_int &b) {
    return mod_int(a) /= b;
  }
  mod_int &operator++() {
    val = val == MOD - 1 ? 0 : val + 1;
    return *this;
  }
  mod_int &operator--() {
    val = val == 0 ? MOD - 1 : val - 1;
    return *this;
  }
  mod_int operator++(int32_t) {
    mod_int before = *this;
    ++*this;
    return before;
  }
  mod_int operator--(int32_t) {
    mod_int before = *this;
    --*this;
    return before;
  }
  mod_int operator-() const { return val == 0 ? 0 : MOD - val; }
  bool operator==(const mod_int &other) const { return val == other.val; }
  bool operator!=(const mod_int &other) const { return val != other.val; }
  mod_int inv() const { return mod_inv(val); }
  friend ostream &operator<<(ostream &stream, const mod_int &m) {
    return stream << m.val;
  }
  friend istream &operator>>(istream &stream, mod_int &m) {
    return stream >> m.val;
  }

  mod_int pow(long long p) const { // dont put a negetive p
    assert(p >= 0);
    mod_int a = *this, result = 1;
    while (p > 0) {
      if (p & 1)
        result *= a;

      a *= a;
      p >>= 1;
    }
    return result;
  }
};
mod_int fact[maxn];
bool fbb = 0;
void build() {
  fbb = 1;
  fact[0] = 1;
  rep(i, 1, maxn) fact[i] = i * fact[i - 1];
}
mod_int C(int n, int r) {
  if (fbb)
    return (fact[n] / (fact[r] * fact[n - r]));
  mod_int a = 1, b = 1;
  rep(i, 0, r) {
    a *= n - i;
    b *= i + 1;
  }
  return a / b;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vi pos, neg;
  rep(i, 0, n) {
    int x;
    cin >> x;
    if (x < 0)
      neg.pb(-x);
    else
      pos.pb(x);
  }
  sort(all(neg));
  sort(all(pos));
  reverse(all(neg));
  reverse(all(pos));

  mod_int ans = 1;

  if (n == k) {
    for (int i : pos)
      ans *= i;
    for (int i : neg)
      ans *= -i;
    cout << ans;
    return;
  }
  if (!sz(pos)) {
    if (k & 1)
      reverse(all(neg));
    rep(i, 0, k) ans *= -neg[i];
    cout << ans;
    return;
  }
  int x = k, i = 1, j = 0;
  for (i = 1; i < sz(neg) and k; i += 2, k -= 2)
    ans *= neg[i] * neg[i - 1];
  while (k)
    ans *= pos[j++], k--;
  i -= 2;
  while (i > 0 and j < sz(pos) - 1) {
    if (neg[i] * neg[i - 1] < pos[j] * pos[j + 1]) {
      ans *= pos[j] * pos[j + 1];
      ans /= neg[i] * neg[i - 1];
      j += 2;
      i -= 2;
    } else
      break;
  }
  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tc = 1, k = 1;
  // cin>>tc;
  while (tc--) {
    // cout<<"Case #"<<k++<<": ";
    solve();
  }
  return 0;
}