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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<int>;
const int mod = 1e9 + 7;
struct mint {
  ll x; // typedef long long ll;
  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;
  }

  // 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 { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
using P = pair<mint, bool>;

int main() {
  int n, k;
  cin >> n >> k;
  vec a, b;
  rep(i, n) {
    int ab;
    cin >> ab;
    if (ab >= 0) {
      a.push_back(ab);
    } else if (ab < 0) {
      b.push_back(ab);
    }
  }

  if (k == n) {
    mint pro = 1;
    for (int aa : a) {
      pro *= aa;
    }
    for (int bb : b) {
      pro *= bb;
    }
    cout << pro << endl;
  } else if (a.empty() && k & 1) {
    sort(all(b), greater<int>());
    mint pro = 1;
    rep(i, k) { pro *= b[i]; }
    cout << pro << endl;
  } else {
    sort(all(b));
    sort(all(a), greater<int>());
    int ai = 0, bi = 0;
    mint pro = 1;
    if (k & 1) {
      pro *= a[ai++];
    }
    while (ai + bi <= k - 2) {
      if (ai >= a.size()) {
        pro *= b[bi++];
        pro *= b[bi++];
      } else if (bi >= b.size()) {
        pro *= a[ai++];
        pro *= a[ai++];
      } else {
        ll a2 = a[ai];
        a2 *= a[ai + 1];
        ll b2 = b[bi];
        b2 *= b[bi + 1];
        if (a2 > b2) {
          pro *= a[ai++];
          pro *= a[ai++];
        } else {
          pro *= b[bi++];
          pro *= b[bi++];
        }
      }
    }
    cout << pro << endl;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<int>;
const int mod = 1e9 + 7;
struct mint {
  ll x; // typedef long long ll;
  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;
  }

  // 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 { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
using P = pair<mint, bool>;

int main() {
  int n, k;
  cin >> n >> k;
  vec a, b;
  rep(i, n) {
    int ab;
    cin >> ab;
    if (ab >= 0) {
      a.push_back(ab);
    } else if (ab < 0) {
      b.push_back(ab);
    }
  }

  if (k == n) {
    mint pro = 1;
    for (int aa : a) {
      pro *= aa;
    }
    for (int bb : b) {
      pro *= bb;
    }
    cout << pro << endl;
  } else if (a.empty() && k & 1) {
    sort(all(b), greater<int>());
    mint pro = 1;
    rep(i, k) { pro *= b[i]; }
    cout << pro << endl;
  } else {
    sort(all(b), greater<int>());
    sort(all(a), greater<int>());
    int ai = 0, bi = 0, ii = 0;
    mint pro = 1;
    while (ii < k - 2) {
      if (ai >= a.size() - 1) {
        pro *= b[bi++];
        pro *= b[bi++];
        ii += 2;
      } else if (bi >= b.size() - 1) {
        pro *= a[ai++];
        ii++;
      } else {
        ll a2 = a[ai];
        a2 *= a[ai + 1];
        ll b2 = b[bi];
        b2 *= b[bi + 1];
        if (a2 > b2) {
          pro *= a[ai++];
          ii++;
        } else {
          pro *= b[bi++];
          pro *= b[bi++];
          ii += 2;
        }
      }
    }
    if (ii == k - 2) {
      pro *= a[ai];
    }
    cout << pro << endl;
  }
}