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

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
#define pll pair<ll, ll>
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll a[220000];
vector<ll> p, m, r;
int main() {
  ll n, k, ans = 1, tmp = 0;
  cin >> n >> k;
  rep(i, 0, n) {
    cin >> tmp;
    tmp >= 0 ? p.pb(tmp) : m.pb(tmp);
  }
  sort(p.rbegin(), p.rend());
  sort(m.begin(), m.end());

  if (p.size() == 0 && k % 2 == 1) {

    irep(i, n - 1, n - k) r.pb(m[i]);
  } else if (k == n) {
    rep(i, 0, p.size()) r.pb(p[i]);
    rep(i, 0, m.size()) r.pb(m[i]);
  } else if (k % 2 == 0) {
    ll cnt = 0;
    ll ppos = 0, mpos = 0;
    while (cnt < k) {
      ll pv = -1, mv = -1;
      if (p.size() > ppos + 1)
        pv = p[ppos] * p[ppos + 1];
      if (m.size() > mpos + 1)
        mv = m[mpos] * m[mpos + 1];

      if (pv >= mv) {
        r.pb(p[ppos]);
        r.pb(p[ppos + 1]);
        ppos += 2;
        cnt += 2;
      } else {
        r.pb(m[mpos]);
        r.pb(m[mpos + 1]);
        mpos += 2;
        cnt += 2;
      }
    }
  } else {
    r.pb(p[0]);
    ll cnt = 1;
    ll ppos = 1, mpos = 0;
    while (cnt < k) {
      ll pv = -1, mv = -1;
      if (p.size() > ppos + 1)
        pv = p[ppos] * p[ppos + 1];
      if (m.size() > mpos + 1)
        mv = m[mpos] * m[mpos + 1];

      if (pv >= mv) {
        r.pb(p[ppos]);
        r.pb(p[ppos + 1]);
        ppos += 2;
        cnt += 2;
      } else {
        r.pb(m[mpos]);
        r.pb(m[mpos + 1]);
        mpos += 2;
        cnt += 2;
      }
    }
  }
  rep(i, 0, r.size()) ans = (ans * r[i]) % mod;
  cout << (mod + ans) % mod << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
#define pll pair<ll, ll>
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll a[220000];
vector<ll> m;
int main() {
  ll n, k, ans = 0, pos0 = 0, pos1 = 0;
  cin >> n >> k;
  rep(i, 0, n) cin >> a[i];
  sort(a, a + n);
  bool allm = false;
  if (a[n - 1] < 0)
    allm = true;

  if (allm && k % 2 == 1) {
    ll tmp = 1;
    irep(i, n - 1, n - k) {
      tmp = (tmp * a[i]);
      if (tmp > 0)
        tmp %= mod;
    }
    cout << (mod + tmp) % mod;
  } else if (k < n) {

    pos0 = 0, pos1 = n - 1;
    ll cnt = 0;
    while (cnt < k / 2) {
      ll tmp0 = -1e9, tmp1 = -1e9;
      if (a[pos0 + 1] < 0)
        tmp0 = a[pos0] * a[pos0 + 1];
      if (a[pos1 - 1] >= 0)
        tmp1 = a[pos1] * a[pos1 - 1];
      if (tmp0 > tmp1) {
        m.pb(tmp0);
        pos0 += 2;
      } else if (tmp0 <= tmp1 && tmp0 != -1e9) {
        m.pb(tmp1);
        pos1 -= 2;
      }
      cnt++;
    }
    if (k % 2 == 0) {
      ll tmp = 1;
      rep(i, 0, k / 2) tmp = (tmp * (m[i] % mod)) % mod;
      cout << (mod + tmp) % mod << endl;
    } else {
      ll tmp = 1;
      rep(i, 0, k / 2) tmp = (tmp * (m[i] % mod)) % mod;
      tmp = (tmp * a[pos1]) % mod;
      cout << (mod + tmp) % mod;
    }
  } else {
    ll tmp = 1;
    rep(i, 0, n) tmp = (tmp * a[i]) % mod;
    cout << (mod + tmp) % mod;
  }
}