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

#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* struct for mod Calculation*/
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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= 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 {
    mint res(*this);
    return res /= a;
  }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
/* global variables */
/* function */
/* main */
int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  vector<ll> s, t;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] >= 0)
      s.push_back(A[i]);
    else
      t.push_back(A[i]);
  }

  int S = s.size(), T = t.size();
  bool ok = false;
  if (S > 0) {
    if (N == K)
      ok = (T % 2 == 0);
    else
      ok = true;
  } else {
    ok = (K % 2 == 0);
  }

  mint ans = 1;
  if (!ok) {
    sort(A.begin(), A.end(), [](int x, int y) { return abs(x) < abs(y); });
    for (int i = 0; i < K; i++)
      ans *= A[i];
  } else {
    sort(s.begin(), s.end());
    sort(t.rbegin(), t.rend());
    if (K % 2 == 1) {
      ans *= s.back();
      s.pop_back();
    }
    vector<ll> p;
    while (s.size() >= 2) {
      ll x = s.back();
      s.pop_back();
      x *= s.back();
      s.pop_back();
      p.push_back(x);
    }
    while (t.size() >= 2) {
      ll x = t.back();
      t.pop_back();
      x *= t.back();
      t.pop_back();
      p.push_back(x);
    }
    sort(p.rbegin(), p.rend());
    for (int i = 0; i < K / 2; i++)
      ans *= p[i];
  }
  cout << ans << '\\n';
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* struct for mod Calculation*/
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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= 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 {
    mint res(*this);
    return res /= a;
  }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
/* global variables */
/* function */
/* main */
int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  vector<ll> s, t;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (A[i] >= 0)
      s.push_back(A[i]);
    else
      t.push_back(A[i]);
  }

  int S = s.size(), T = t.size();
  bool ok = false;
  if (S > 0) {
    if (N == K)
      ok = (T % 2 == 0);
    else
      ok = true;
  } else {
    ok = (K % 2 == 0);
  }

  mint ans = 1;
  if (!ok) {
    sort(A.begin(), A.end(), [](int x, int y) { return abs(x) < abs(y); });
    for (int i = 0; i < K; i++)
      ans *= A[i];
  } else {
    sort(s.begin(), s.end());
    sort(t.rbegin(), t.rend());
    if (K % 2 == 1) {
      ans *= s.back();
      s.pop_back();
    }
    vector<ll> p;
    while (s.size() >= 2) {
      int x = s.back();
      s.pop_back();
      x *= s.back();
      s.pop_back();
      p.push_back(x);
    }
    while (t.size() >= 2) {
      int x = t.back();
      t.pop_back();
      x *= t.back();
      t.pop_back();
      t.push_back(x);
    }
    sort(p.rbegin(), p.rend());
    for (int i = 0; i < K / 2; i++)
      ans *= p[i];
  }
  cout << ans << '\\n';
}