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

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod

ll Solve() {
  ll N, K;
  cin >> N >> K;
  if (N == K) {
    ll ret = 1;
    rep(i, N) {
      ll x;
      cin >> x;
      if (x == 0)
        return 0;
      if (x < 0)
        x += mod;
      MUL(ret, x);
    }
    return ret;
  }
  vec A, B;
  ll Count = 0;
  rep(i, N) {
    ll x;
    cin >> x;
    if (x > 0)
      A.pb(x);
    if (x < 0)
      B.pb(-x);
    if (x == 0)
      Count++;
  }
  if (N - Count < K)
    return 0;
  if (A.size() == 0) {
    sort(ALL(B));
    if (K % 2) {
      if (Count)
        return 0;
      ll ret = 1;
      rep(i, K) MUL(ret, B[i]);
      return mod - ret;
    }
    reverse(ALL(B));
    ll ret = 1;
    rep(i, K) MUL(ret, B[i]);
    return ret;
  }
  sort(ALL(A));
  sort(ALL(B));
  ll X = min((ll)A.size(), K);
  if (X % 2 != K % 2)
    X--;
  ll Y = K - X;
  if (Y > B.size()) {
    if (A.size() + B.size() < N)
      return 0;
    ll ret = 1;
    rep(i, B.size()) MUL(ret, B[i]);
    rep(i, A.size()) MUL(ret, A[i]);
    return mod - ret;
  }
  reverse(ALL(B));
  reverse(ALL(A));
  while (true) {
    if (X < 2)
      break;
    if (Y + 1 >= B.size())
      break;
    if (B[Y] * B[Y + 1] <= A[X - 2] * A[X - 1])
      break;
    X -= 2;
    Y += 2;
  }
  ll ret = 1;
  rep(i, X) MUL(ret, A[i]);
  rep(i, Y) MUL(ret, B[i]);
  return ret;
}
int main() { cout << Solve() << endl; }

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod
#define MUL(a, b) a = (a * b) % mod

ll Solve() {
  ll N, K;
  cin >> N >> K;
  if (N == K) {
    ll ret = 1;
    rep(i, N) {
      ll x;
      cin >> x;
      if (x < 0)
        x += mod;
      MUL(ret, x);
    }
    return ret;
  }
  vec A, B;
  rep(i, N) {
    ll x;
    cin >> x;
    if (x > 0)
      A.pb(x);
    if (x < 0)
      B.pb(-x);
  }

  if (K % 2 && B.size() == N) {
    sort(ALL(B));
    ll ret = 1;
    rep(i, K) MUL(ret, B[i]);
    return mod - ret;
  }
  if (A.size() + B.size() < K)
    return 0;
  sort(ALL(A));
  sort(ALL(B));
  ll X = max((ll)A.size(), K);
  if (X % 2 != K % 2)
    X--;
  ll Y = K - X;
  if (Y > B.size()) {
    if (A.size() + B.size() < N)
      return 0;
    ll ret = 1;
    rep(i, B.size()) MUL(ret, B[i]);
    rep(i, A.size()) MUL(ret, A[i]);
    return mod - ret;
  }
  reverse(ALL(B));
  reverse(ALL(A));
  while (true) {
    if (X < 2)
      break;
    if (Y + 1 >= B.size())
      break;
    if (B[Y] * B[Y + 1] <= A[X - 2] * A[X - 1])
      break;
    X -= 2;
    Y += 2;
  }
  ll ret = 1;
  rep(i, X) MUL(ret, A[i]);
  rep(i, Y) MUL(ret, B[i]);
  return ret;
}
int main() { cout << Solve() << endl; }