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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  bool allm = 1;
  rep(i, n) {
    cin >> a[i];
    if (a[i] >= 0)
      allm = 0;
  }
  if ((k % 2 == 1 && allm) || k == n) {
    sort(all(a));
    reverse(all(a));
    ll ans = 1;
    rep(i, k) { ans = (ans * a[i]) % mod; }
    cout << (ans + mod) % mod << endl;
    return 0;
  }
  vector<pll> absv(n);
  rep(i, n) { absv[i] = pll(abs(a[i]), a[i]); }
  sort(all(absv));
  reverse(all(absv));
  ll mminid = -1, pminid = -1;
  rep(i, k) {
    if (absv[i].second < 0)
      mminid = i;
    else
      pminid = i;
  }
  ll mmaxid = -1, pmaxid = -1;
  for (int i = k; i < n; i++) {
    if (mmaxid == -1 && absv[i].second < 0)
      mmaxid = i;
    if (pmaxid == -1 && absv[i].second >= 0)
      pmaxid = i;
  }
  ll sum = 1, sum1 = -inf, sum2 = -inf;
  rep(i, k) { sum = (sum * absv[i].second) % mod; }
  if (mminid != -1 && pmaxid != -1) {
    sum1 = 1;
    rep(i, k) {
      if (i != mminid)
        sum1 = (sum1 * absv[i].second) % mod;
    }
    sum1 = (sum1 * absv[pmaxid].second) % mod;
  }
  if (pminid != -1 && mmaxid != -1) {
    sum2 = 1;
    rep(i, k) {
      if (i != pminid)
        sum2 = (sum2 * absv[i].second) % mod;
    }
    sum2 = (sum2 * absv[mmaxid].second) % mod;
  }
  if (sum >= 0) {
    cout << sum << endl;
    return 0;
  }
  if (sum1 == -inf) {
    cout << sum2 << endl;
  } else if (sum2 == -inf) {
    cout << sum1 << endl;
  } else if (sum1 != -inf && sum2 != -inf &&
             absv[pmaxid].first * absv[pminid].first >=
                 absv[mmaxid].first * absv[mminid].first) {
    cout << sum1 << endl;
  } else {
    cout << sum2 << endl;
  }
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  bool allm = 1;
  rep(i, n) {
    cin >> a[i];
    if (a[i] >= 0)
      allm = 0;
  }
  if (allm || k == n) {
    sort(all(a));
    reverse(all(a));
    ll ans = 1;
    rep(i, k) { ans = (ans * a[i]) % mod; }
    cout << (ans + mod) % mod << endl;
    return 0;
  }
  vector<pll> absv(n);
  rep(i, n) { absv[i] = pll(abs(a[i]), a[i]); }
  sort(all(absv));
  reverse(all(absv));
  ll mminid = -1, pminid = -1;
  rep(i, k) {
    if (absv[i].second < 0)
      mminid = i;
    else
      pminid = i;
  }
  ll mmaxid = -1, pmaxid = -1;
  for (int i = k; i < n; i++) {
    if (mmaxid == -1 && absv[i].second < 0)
      mmaxid = i;
    if (pmaxid == -1 && absv[i].second >= 0)
      pmaxid = i;
  }
  ll sum = 1, sum1 = -inf, sum2 = -inf;
  rep(i, k) { sum = (sum * absv[i].second) % mod; }
  if (mminid != -1 && pmaxid != -1) {
    sum1 = 1;
    rep(i, k) {
      if (i != mminid)
        sum1 = (sum1 * absv[i].second) % mod;
    }
    sum1 = (sum1 * absv[pmaxid].second) % mod;
  }
  if (pminid != -1 && mmaxid != -1) {
    sum2 = 1;
    rep(i, k) {
      if (i != pminid)
        sum2 = (sum2 * absv[i].second) % mod;
    }
    sum2 = (sum2 * absv[mmaxid].second) % mod;
  }
  if (sum >= 0) {
    cout << sum << endl;
    return 0;
  }
  if (sum1 != -inf && (ll)absv[pmaxid].first * absv[pminid].first <=
                          (ll)absv[mmaxid].first * absv[mminid].first) {
    cout << sum1 << endl;
  } else {
    cout << sum2 << endl;
  }
}