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

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;

bool cmp(int x, int y) { return abs(x) > abs(y); }

ll bm(ll b, ll p) {
  ll rt = 1;
  for (ll i = 1, j = b; i <= p; i <<= 1, j = j * j % MOD)
    if (p & i)
      rt = rt * j % MOD;
  return rt;
}

int main() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  int cp = 0, cn = 0, c0 = 0;
  FOR(i, 0, n - 1) {
    cin >> a[i];
    if (a[i] == 0)
      c0++;
    else if (a[i] < 0)
      cn++;
    else
      cp++;
  }
  sort(a.begin(), a.end(), cmp);
  ll ans = 1;
  int mnp = k & 1, mxn = cn - (cn & 1);
  if (mxn + cp < k || cp < mnp) {
    if (c0)
      ans = 0;
    else {
      ROF(i, (int)a.size() - 1, (int)a.size() - k)
      ans = ans * a[i] % MOD;
    }
  } else {
    ll ln = 0, lp = 0;
    FOR(i, 0, k - 1) {
      ans = ans * a[i] % MOD;
      if (a[i] < 0)
        ln = a[i];
      else
        lp = a[i];
    }
    if (ans < 0) {
      ll mn = 0, mp = 0;
      ROF(i, (int)a.size() - 1, k) {
        if (a[i] < 0)
          mn = a[i];
        else
          mp = a[i];
      }
      if (lp == 0 || mp * lp > mn * ln) {
        ans = ans * bm(ln, MOD - 2) % MOD;
        ans = ans * mp % MOD;
      } else {
        ans = ans * bm(lp, MOD - 2) % MOD;
        ans = ans * mn % MOD;
      }
    }
  }
  cout << (ans + MOD) % MOD << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;

bool cmp(int x, int y) { return abs(x) > abs(y); }

ll bm(ll b, ll p) {
  ll rt = 1;
  for (ll i = 1, j = b; i <= p; i <<= 1, j = j * j % MOD)
    if (p & i)
      rt = rt * j % MOD;
  return rt;
}

int main() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  int cp = 0, cn = 0, c0 = 0;
  FOR(i, 0, n - 1) {
    cin >> a[i];
    if (a[i] == 0)
      c0++;
    else if (a[i] < 0)
      cn++;
    else
      cp++;
  }
  sort(a.begin(), a.end(), cmp);
  ll ans = 1;
  int mnp = k & 1, mxn = cn - (cn & 1);
  if (mxn + cp < k || cp < mnp) {
    assert(1 == 2);
    if (c0)
      ans = 0;
    else {
      ROF(i, (int)a.size() - 1, (int)a.size() - k)
      ans = ans * a[i] % MOD;
    }
  } else {
    ll ln = 0, lp = 0;
    FOR(i, 0, k - 1) {
      ans = ans * a[i] % MOD;
      if (a[i] < 0)
        ln = a[i];
      else
        lp = a[i];
    }
    if (ans < 0) {
      ll mn = 0, mp = 0;
      ROF(i, (int)a.size() - 1, k) {
        if (a[i] < 0)
          mn = a[i];
        else
          mp = a[i];
      }
      if (mn * ln > mp * lp) {
        ans = ans * bm(lp, MOD - 2) % MOD;
        ans = ans * mn % MOD;
      } else {
        ans = ans * bm(ln, MOD - 2) % MOD;
        ans = ans * mp % MOD;
      }
    }
  }
  cout << (ans + MOD) % MOD << endl;
}