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

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << "\\t|";
  __f(comma + 1, args...);
}

#define print(x)                                                               \\
  for (auto it : x)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
typedef long long ll;
typedef unsigned long long ull;

const int MOD = 1e9 + 7;
ll mod(ll a) { return (ll)(a % MOD + MOD) % MOD; }

ll mul(ll a, ll b) { return (ll)mod(mod(a) * mod(b)); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> pos;
  vector<ll> neg;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x > 0)
      pos.push_back(x);
    else
      neg.push_back(x);
  }
  ll ans = 1;
  if ((int)pos.size()) {
    if (k % 2) {
      sort(pos.begin(), pos.end());
      ans = mul(ans, pos.back());
      pos.pop_back();
      --k;
    }
    vector<ll> ok;
    sort(pos.begin(), pos.end(), greater<>());
    sort(neg.begin(), neg.end());
    for (int i = 0; i + 1 < (int)pos.size(); i += 2)
      ok.push_back(pos[i] * pos[i + 1]);
    for (int i = 0; i + 1 < (int)neg.size(); i += 2)
      ok.push_back(neg[i] * neg[i + 1]);
    if ((int)pos.size() % 2 && (int)neg.size() % 2)
      ok.push_back(pos.back() * neg.back());
    sort(ok.begin(), ok.end(), greater<>());
    for (int i = 0; i < (k / 2); i++)
      ans = mul(ans, ok[i]);
    cout << ans;
  } else {
    sort(neg.begin(), neg.end());
    if (k % 2)
      reverse(neg.begin(), neg.end());
    for (int i = 0; i < k; i++)
      ans = mul(ans, neg[i]);
    cout << ans;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << "\\t|";
  __f(comma + 1, args...);
}

#define print(x)                                                               \\
  for (auto it : x)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
typedef long long ll;
typedef unsigned long long ull;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  int mod = 1e9 + 7;

  vector<int> v(n);
  bool all_negative = true;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    if (v[i] >= 0) {
      all_negative = false;
    }
  }
  ll ans = 1;
  if (all_negative) {
    if (k % 2) {
      sort(v.begin(), v.end(), greater<int>());
    } else {
      sort(v.begin(), v.end());
    }
    for (int i = 0; i < k; i++) {
      ans = (ll)((ans % mod) * (v[i] % mod)) % mod;
    }
    cout << ((ans + mod) % mod) << '\\n';
    return 0;
  }

  vector<int> pos, neg;
  for (int i = 0; i < n; i++) {
    if (v[i] >= 0) {
      pos.push_back(v[i]);
    } else {
      neg.push_back(v[i]);
    }
  }
  sort(pos.begin(), pos.end(), greater<int>());
  sort(neg.begin(), neg.end());
  for (int i = 0; i < k && i < pos.size(); i++) {
    ans = (ll)((ans % mod) * (pos[i] % mod)) % mod;
  }

  int i = min((int)pos.size() - 1, k);
  int j = 0;
  while ((i > 0) && (j < (int)neg.size() - 1)) {
    if (pos[i] * pos[i - 1] > neg[j] * neg[j + 1]) {
      break;
    }
    int _old = ((pos[i] % mod) * (pos[i - 1] % mod)) % mod;
    int _new = ((neg[j] % mod) * (neg[j + 1] % mod));
    ans = (ans / _old) % mod;
    ans = (ans * _new) % mod;
    i -= 2;
    j += 2;
  }

  cout << ans << '\\n';
  return 0;
}