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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
struct edge {
  ll s, t, x;
};
typedef pair<ll, ll> P;
typedef pair<ll, P> event;
const ll MAX = 510000;
const ll MOD = 1000000007;
#define INF (ll)1000000000000

int main() {
  ll N, K;
  cin >> N >> K;
  vector<P> A(N);
  int memoflag = 0;
  int cnt = 0;
  rep(i, N) {
    ll g;
    cin >> g;
    if (g < 0) {
      A[i] = P(-1ll * g, -1);
      cnt++;
    } else {
      A[i] = P(g, 1);
      if (g > 0) {
        memoflag = 1;
      }
    }
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  stack<ll> sei;
  stack<ll> hu;
  rep(i, K) {
    auto t = A[i];
    if (t.second == 1) {
      sei.push(t.first);
    } else {
      hu.push(t.first);
    }
  }
  queue<ll> SS;
  queue<ll> HH;
  for (int i = K; i < N; i++) {
    auto t = A[i];
    if (t.second == 1) {
      SS.push(t.first);
    } else {
      HH.push(t.first);
    }
  }
  ll ans = 1ll;
  // 積が負になる例外処理
  if (memoflag == 0 && K % 2 == 1) {
    reverse(A.begin(), A.end());
    rep(i, K) {
      ans *= A[i].first * (-1ll);
      ans += MOD;
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }
  if (memoflag == 0 && (cnt < K)) {

    cout << 0 << endl;
    return 0;
  }
  // このままかけて正になるとき
  if (hu.size() % 2 == 0) {
    while (!sei.empty()) {
      ans *= sei.top();
      sei.pop();
      ans %= MOD;
    }
    while (!hu.empty()) {
      ans *= hu.top();
      hu.pop();
      ans %= MOD;
    }
    ans += MOD;
    ans %= MOD;
    cout << ans;
    return 0;
  }
  // このままだと負のとき
  if (hu.size() % 2 == 1) {
    if (sei.empty()) {
      sei.push(SS.front());
      hu.pop();
    } // ok
    else if (SS.empty() && !HH.empty()) {
      sei.pop();
      hu.push(HH.front());
    } // ok
    else if (HH.empty() && !SS.empty()) {
      hu.pop();
      sei.push(SS.front());
    } // ok
    else if (HH.empty() && SS.empty()) {
    } else {
      ll g = SS.front() * sei.top();
      ll h = HH.front() * hu.top();
      if (g > h) {
        hu.pop();
        sei.push(SS.front());
      } else {
        sei.pop();
        hu.push(HH.front());
      }
    }
  }
  while (!sei.empty()) {
    ans *= sei.top();
    sei.pop();
    ans %= MOD;
  }
  while (!hu.empty()) {
    ans *= (-1ll) * hu.top();
    hu.pop();
    ans += MOD;
    ans %= MOD;
    ans += MOD;
    ans %= MOD;
  }
  ans += MOD;
  ans %= MOD;
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
struct edge {
  ll s, t, x;
};
typedef pair<ll, ll> P;
typedef pair<ll, P> event;
const ll MAX = 510000;
const ll MOD = 1000000007;
#define INF (ll)1000000000000

int main() {
  ll N, K;
  cin >> N >> K;
  vector<P> A(N);
  rep(i, N) {
    ll g;
    cin >> g;
    if (g < 0) {
      A[i] = P(-1ll * g, -1);
    } else {
      A[i] = P(g, 1);
    }
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  stack<ll> sei;
  stack<ll> hu;
  rep(i, K) {
    auto t = A[i];
    if (t.second == 1) {
      sei.push(t.first);
    } else {
      hu.push(t.first);
    }
  }
  queue<ll> SS;
  queue<ll> HH;
  for (int i = K; i < N; i++) {
    auto t = A[i];
    if (t.second == 1) {
      SS.push(t.first);
    } else {
      HH.push(t.first);
    }
  }
  ll ans = 1ll;
  if (hu.size() % 2 == 1) {
    if (SS.empty() && !HH.empty()) {
      hu.pop();
      sei.push(SS.front());
    } else if (HH.empty() && !SS.empty()) {
      sei.pop();
      hu.push(HH.front());
    } else {
      ll g = SS.front() * sei.top();
      ll h = HH.front() * hu.top();
      if (g > h) {
        hu.pop();
        sei.push(SS.front());
      } else {
        sei.pop();
        hu.push(HH.front());
      }
    }
  }
  while (!sei.empty()) {
    ans *= sei.top();
    sei.pop();
    ans %= MOD;
  }
  while (!hu.empty()) {
    ans *= hu.top();
    hu.pop();
    ans %= MOD;
  }
  cout << ans;
}