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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

#define int ll

const int mod = 1e9 + 7;

template <int Modulus> struct modint {
  ll x; // typedef long long ll;
  modint(ll x = 0) : x((x % mod + mod) % mod) {}
  modint operator-() const { return modint(-x); }
  modint &operator+=(const modint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  modint &operator-=(const modint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  modint &operator*=(const modint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  modint operator+(const modint a) const { return modint(*this) += a; }
  modint operator-(const modint a) const { return modint(*this) -= a; }
  modint operator*(const modint a) const { return modint(*this) *= a; }
  modint pow(ll t) const {
    if (!t)
      return 1;
    modint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  modint inv() const { return pow(mod - 2); }
  modint &operator/=(const modint a) { return *this *= a.inv(); }
  modint operator/(const modint a) const { return modint(*this) /= a; }
};
template <int Modulus> istream &operator>>(istream &is, modint<Modulus> &a) {
  return is >> a.x;
}
template <int Modulus>
ostream &operator<<(ostream &os, const modint<Modulus> &a) {
  return os << a.x;
}

using mint = modint<mod>;

signed main() {
  ll n, k;
  bool an = true;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll &i : a) {
    cin >> i;
    if (i >= 0)
      an = false;
  }
  if (an && k % 2 || n == k) {
    sort(a.begin(), a.end(), greater<>());
    mint r = 1;
    for (int i = 0; i < k; ++i)
      r *= a[i];
    cout << r;
    return 0;
  }
  sort(a.begin(), a.end(), [](ll l, ll r) { return abs(l) > abs(r); });
  vector<ll> pos, neg;
  for (int i = 0; i < n; ++i) {
    if (a[i] >= 0)
      pos.push_back(a[i]);
    else
      neg.push_back(-a[i]);
  }
  int sp = 0, sn = 0, tk = k;
  mint r = 1;
  for (int i = 0; i < n && tk > 0; ++i) {
    if (tk <= 1) {
      if (pos.size() - sp >= 1)
        r *= pos[sp++], --tk;
      else
        break;
    } else if (neg.size() - sn <= 1)
      r *= pos[sp++], --tk;
    else if (pos.size() - sp == 0) {
      r *= neg[sn++];
      --tk;
      r *= neg[sn++];
      --tk;
    } else {
      if (pos[sp] * pos[sp + 1] >= neg[sn] * neg[sn + 1])
        r *= pos[sp++], --tk;
      else {
        r *= neg[sn++];
        --tk;
        r *= neg[sn++];
        --tk;
      }
    }
  }
  assert(tk <= 1);
  cout << r;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

#define int ll

const int mod = 1e9 + 7;

template <int Modulus> struct modint {
  ll x; // typedef long long ll;
  modint(ll x = 0) : x((x % mod + mod) % mod) {}
  modint operator-() const { return modint(-x); }
  modint &operator+=(const modint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  modint &operator-=(const modint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  modint &operator*=(const modint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  modint operator+(const modint a) const { return modint(*this) += a; }
  modint operator-(const modint a) const { return modint(*this) -= a; }
  modint operator*(const modint a) const { return modint(*this) *= a; }
  modint pow(ll t) const {
    if (!t)
      return 1;
    modint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  modint inv() const { return pow(mod - 2); }
  modint &operator/=(const modint a) { return *this *= a.inv(); }
  modint operator/(const modint a) const { return modint(*this) /= a; }
};
template <int Modulus> istream &operator>>(istream &is, modint<Modulus> &a) {
  return is >> a.x;
}
template <int Modulus>
ostream &operator<<(ostream &os, const modint<Modulus> &a) {
  return os << a.x;
}

using mint = modint<mod>;

signed main() {
  ll n, k;
  bool an = true;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll &i : a) {
    cin >> i;
    if (i >= 0)
      an = false;
  }
  if (an && k % 2) {
    sort(a.begin(), a.end(), greater<>());
    mint r = 1;
    for (int i = 0; i < k; ++i)
      r *= a[i];
    cout << r;
    return 0;
  }
  sort(a.begin(), a.end(), [](ll l, ll r) { return abs(l) > abs(r); });
  vector<ll> pos, neg;
  for (int i = 0; i < n; ++i) {
    if (a[i] >= 0)
      pos.push_back(a[i]);
    else
      neg.push_back(-a[i]);
  }
  int sp = 0, sn = 0, tk = k, allp = pos.size();
  ll pt = -1;
  mint r = 1;
  if (allp % 2 == tk % 2) {
    pt = sp++, --tk;
  }
  for (int i = 0; i < n && tk > 0; ++i) {
    if (neg.size() - sn <= 1) {
      if (pt == -1)
        pt = sp++;
      else {
        r *= pos[pt], --tk;
        r *= pos[sp++], --tk;
      }
    } else if (pos.size() - sp <= 1) {
      r *= neg[sn++], --tk;
      r *= neg[sn++], --tk;
    } else {
      if (pos[sp] * pos[sp + 1] >= neg[sn] * neg[sn + 1]) {
        if (pt == -1)
          pt = sp++;
        else {
          r *= pos[pt], --tk;
          r *= pos[sp++], --tk;
        }
      } else {
        r *= neg[sn++], --tk;
        r *= neg[sn++], --tk;
      }
    }
  }
  if (pt != -1) {
    r *= pos[pt], --tk;
  }
  cout << r;
}