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

#include <bits/stdc++.h>
#include <math.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M_PI 3.14159265358979323846

const int MOD = 1e9 + 7;
ll mod(ll x) { return (x % MOD + MOD) % MOD; }
ll mul(ll a, ll b) { return mod(mod(a) * mod(b)); }

void solve() {
  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;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(10);
  int t = 1;
  //        cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define M_PI 3.14159265358979323846

const int MOD = 1e9 + 7;
ll mod(ll x) { return (x % MOD + MOD) % MOD; }
ll mul(ll a, ll b) { return mod(mod(a) * mod(b)); }

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> pos;
  vector<int> neg;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x > 0)
      pos.push_back(x);
    else
      neg.push_back(x);
  }
  int ans = 1;
  if ((int)pos.size()) {
    if (k & 1) {
      sort(pos.begin(), pos.end());
      ans = mul(ans, pos.back());
      pos.pop_back();
      --k;
    }
    vector<ll> pos;
    sort(pos.begin(), pos.end(), greater<>());
    sort(neg.begin(), neg.end());
    for (int i = 0; i + 1 < (int)pos.size(); i += 2)
      pos.push_back((ll)pos[i] * (ll)pos[i + 1]);
    for (int i = 0; i + 1 < (int)neg.size(); i += 2)
      pos.push_back((ll)neg[i] * (ll)neg[i + 1]);
    if ((int)pos.size() % 2 && (int)neg.size() % 2)
      pos.push_back((ll)pos.back() * (ll)neg.back());
    sort(pos.begin(), pos.end(), greater<>());
    for (int i = 0; i < (k / 2); i++)
      ans = mul(ans, pos[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;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(10);
  int t = 1;
  //        cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}