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

#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x)                                                         \\
  sort(all((x)));                                                              \\
  (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll m = 1e9 + 7;
ll mul(ll x, ll y) {
  ll ans = (x * 1ll * y) % m;
  while (ans < 0)
    ans += m;
  return ans;
}
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n = 0, k = 0;

  cin >> n >> k;

  vector<ll> a(n), pos, neg, ppos, pneg;

  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      pos.pb(a[i]);
    else
      neg.pb(a[i]);
  }

  sort(all(a));

  if (n == k) {
    ll ans = 1;
    for (auto &x : a)
      ans = mul(ans, x);
    cout << ans;
    return 0;
  }

  if (!pos.size()) {
    ll ans = 1;
    if (k % 2 == 0) {
      for (ll i = 0; i < k; i++)
        ans = mul(ans, a[i]);
    } else {
      for (ll i = 0; i < k; i++)
        ans = mul(ans, a[n - i - 1]);
    }
    cout << ans;
    return 0;
  }

  sort(rall(pos));
  sort(all(neg));

  ppos.resize(pos.size() + 1);
  pneg.resize(neg.size() + 1);

  ppos[0] = 1;
  pneg[0] = 1;

  for (ll i = 1; i <= pos.size(); i++)
    ppos[i] = mul(ppos[i - 1], pos[i - 1]);

  for (ll i = 1; i <= neg.size(); i++)
    pneg[i] = mul(pneg[i - 1], neg[i - 1]);

  ll posp = min((ll)pos.size(), k);

  ll negp = k - posp;

  if (negp % 2 == 1) {
    negp++;
    posp--;
  }

  while (negp + 2 <= neg.size() && posp >= 2 &&
         pos[posp - 1] * pos[posp - 2] < neg[negp] * neg[negp + 1]) {
    negp += 2;
    posp -= 2;
  }

  cout << mul(ppos[posp], pneg[negp]);

  return 0;
}
/*

*/


Error: -11

Fixed submission:

// #include <iostream>
#include <algorithm>
#include <array>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x)                                                         \\
  sort(all((x)));                                                              \\
  (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll m = 1e9 + 7;
ll mul(ll x, ll y) { return (x * 1ll * y) % m; }
int main() {

  // ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  ifstream cin;
  ofstream cout;

  cin.open("/Volumes/Samsung_T5/AtCoder/in.txt");
  cout.open("/Volumes/Samsung_T5/AtCoder/out.txt");

  ll n = 0, k = 0;

  cin >> n >> k;

  vector<ll> a(n), pos, neg;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      pos.pb(a[i]);
    else
      neg.pb(a[i]);
  }

  if (neg.size() == n) {
    ll ans = neg[0];
    for (int i = 1; i < n; i++)
      ans = mul(ans, neg[i]);
    while (ans < 0)
      ans += m;
    cout << ans;
  } else if (n == k) {
    ll ansn = 1, ansp = 1;
    for (int i = 0; i < neg.size(); i++)
      ansn = mul(ansn, neg[i]);
    for (int i = 0; i < pos.size(); i++)
      ansp = mul(ansp, pos[i]);
    ll ans = mul(ansn, ansp);
    while (ans < 0)
      ans += m;
    cout << ans;
  } else {
    ll ppos = min((ll)pos.size(), k);
    ll pneg = k - ppos;
    if (pneg % 2 == 1) {
      pneg++;
      ppos--;
    }
    while (pneg + 2 <= neg.size() && ppos >= 2 &&
           pos[ppos - 1] * pos[ppos - 2] << neg[pneg] * neg[pneg + 1]) {
      ppos -= 2;
      pneg += 2;
    }
    ll ansp = 1, ansn = 1;
    for (int i = 0; i < ppos; i++)
      ansp = mul(ansp, pos[i]);
    for (int i = 0; i < pneg; i++)
      ansn = mul(ansn, neg[i]);
    ll ans = mul(ansp, ansn);
    while (ans < 0)
      ans += m;
    cout << ans;
  }

  return 0;
}
/*

*/