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

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (long long i = 0; i < (n); i++)
using ll = long long;
using P = pair<long long, long long>;
template <class T> using vec = vector<T>;
const ll mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  vec<ll> a(n);
  rep(i, n) cin >> a[i];
  ll res = 1;
  if (n == k) {
    rep(i, n) {
      res *= a[i];
      res %= mod;
    }
    cout << (res + mod) % mod << endl;
    return 0;
  }
  sort(a.begin(), a.end());
  if (a[n - 1] <= 0) {
    if (k % 2 == 1)
      reverse(a.begin(), a.end());
    rep(i, k) {
      res *= a[i];
      res %= mod;
    }
    cout << (res + mod) % mod << endl;
    return 0;
  }
  vec<ll> a_p, a_m;
  rep(i, n) {
    if (a[i] < 0)
      a_m.emplace_back(-a[i]);
    else if (a[i] > 0)
      a_p.emplace_back(a[i]);
  }
  reverse(a_p.begin(), a_p.end());
  if (k % 2 == 0) {
    vec<ll> b;
    for (ll i = 1; i < a_p.size(); i += 2)
      b.emplace_back(a_p[i - 1] * a_p[i]);
    for (ll i = 1; i < a_m.size(); i += 2)
      b.emplace_back(a_m[i - 1] * a_m[i]);
    sort(b.rbegin(), b.rend());
    if (b.size() < k / 2) {
      cout << 0 << endl;
      return 0;
    }
    rep(i, k / 2) {
      res *= b[i] % mod;
      res %= mod;
    }
  } else {
    res *= a_p[0];
    vec<ll> b;
    for (ll i = 2; i < a_p.size(); i += 2)
      b.emplace_back(a_p[i - 1] * a_p[i]);
    for (ll i = 1; i < a_m.size(); i += 2)
      b.emplace_back(a_m[i - 1] * a_m[i]);
    sort(b.rbegin(), b.rend());
    if (b.size() < k / 2) {
      cout << 0 << endl;
      return 0;
    }
    rep(i, k / 2) {
      res *= b[i] % mod;
      res %= mod;
    }
  }
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (long long i = 0; i < (n); i++)
using ll = long long;
using P = pair<long long, long long>;
template <class T> using vec = vector<T>;
const ll mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k;
  cin >> n >> k;
  vec<ll> a(n);
  rep(i, n) cin >> a[i];
  ll res = 1;
  if (n == k) {
    rep(i, n) {
      res *= a[i];
      res %= mod;
    }
    cout << (res + mod) % mod << endl;
    return 0;
  }
  sort(a.begin(), a.end());
  if (a[n - 1] <= 0) {
    if (k % 2 == 1)
      reverse(a.begin(), a.end());
    rep(i, k) {
      res *= a[i];
      res %= mod;
    }
    cout << (res + mod) % mod << endl;
    return 0;
  }
  vec<ll> a_p, a_m;
  rep(i, n) {
    if (a[i] < 0)
      a_m.emplace_back(-a[i]);
    else if (a[i] > 0)
      a_p.emplace_back(a[i]);
  }
  reverse(a_p.begin(), a_p.end());
  if (k % 2 == 0) {
    vec<ll> b;
    for (ll i = 0; i < a_p.size() - 1; i += 2)
      b.emplace_back(a_p[i] * a_p[i + 1]);
    for (ll i = 0; i < a_m.size() - 1; i += 2)
      b.emplace_back(a_m[i] * a_m[i + 1]);
    sort(b.rbegin(), b.rend());
    if (b.size() <= k / 2) {
      cout << 0 << endl;
      return 0;
    }
    rep(i, k / 2) {
      res *= b[i] % mod;
      res %= mod;
    }
  } else {
    res *= a_p[0];
    vec<ll> b;
    for (ll i = 1; i < a_p.size() - 1; i += 2)
      b.emplace_back(a_p[i] * a_p[i + 1]);
    for (ll i = 0; i < a_m.size() - 1; i += 2)
      b.emplace_back(a_m[i] * a_m[i + 1]);
    sort(b.rbegin(), b.rend());
    if (b.size() <= k / 2) {
      cout << 0 << endl;
      return 0;
    }
    rep(i, k / 2) {
      res *= b[i] % mod;
      res %= mod;
    }
  }
  cout << res << endl;
}