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

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) v.begin(), v.end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// snippets:tmp,cl,cvl,cs,co,coy,con,cov,setp
const ll mod = 1e9 + 7;

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vll pl;
  vll mi;
  rep(i, n) {
    ll a;
    cin >> a;
    if (a >= 0)
      pl.push_back(a);
    else
      mi.push_back(a);
  }
  sort(rall(pl));
  sort(all(mi));
  if (pl.size() == 0 && k % 2 == 1) {
    sort(rall(mi));
    ll ans = 1;
    rep(i, k) {
      ans *= abs(mi[i]) % mod;
      ans %= mod;
    }
    ans *= -1;
    ans += mod;
    cout << ans % mod << endl;
    return 0;
  }
  if (n == k) {
    ll ans = 1;
    rep(i, pl.size()) {
      ans *= pl[i] % mod;
      ans %= mod;
    }
    rep(i, mi.size()) {
      ans *= abs(mi[i]) % mod;
      ans %= mod;
    }
    if (mi.size() % 2 == 1)
      ans *= -1;
    if (ans < 0)
      ans += mod;
    cout << ans % mod << endl;
    return 0;
  }
  ll i = 0;
  ll j = 0;
  long long ans = 1;
  ll count = 0;
  while (true) {
    if (i + j == k)
      break;
    if (i < pl.size() && (j == mi.size() || pl[i] >= abs(mi[j]))) {
      ans *= pl[i] % mod;
      i++;
      ans %= mod;
    } else {
      ans *= abs(mi[j]) % mod;
      count++;
      j++;
      ans %= mod;
    }
  }
  ans %= mod;
  if (count % 2 == 0)
    cout << ans % mod << endl;
  else {
    if (k != n) {
      if (i != pl.size() && j != mi.size() && j != 0 && i != 0) {
        if (pl[i] * pl[i - 1] > mi[j] * mi[j - 1]) {
          ans = ans * modinv(abs(mi[j - 1]), mod) % mod * pl[i] % mod;
        } else {
          ans = ans * modinv(pl[i - 1], mod) % mod * abs(mi[j]) % mod;
        }
      } else if (i != pl.size() && j != 0) {
        ans = ans * modinv(abs(mi[j - 1]), mod) % mod * pl[i] % mod;
      } else {
        ans = ans * modinv(pl[i - 1], mod) % mod * abs(mi[j]) % mod;
      }
    }
    if (ans < 0)
      ans += mod;
    cout << ans % mod << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) v.begin(), v.end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// snippets:tmp,cl,cvl,cs,co,coy,con,cov,setp
const ll mod = 1e9 + 7;

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vll pl;
  vll mi;
  rep(i, n) {
    ll a;
    cin >> a;
    if (a >= 0)
      pl.push_back(a);
    else
      mi.push_back(a);
  }
  sort(rall(pl));
  sort(all(mi));
  if (pl.size() == 0 && k % 2 == 1) {
    sort(rall(mi));
    ll ans = 1;
    rep(i, k) {
      ans *= mi[i] % mod;
      ans %= mod;
    }
    if (ans < 0)
      ans += mod;
    cout << ans % mod << endl;
    return 0;
  }
  if (n == k) {
    ll ans = 1;
    rep(i, pl.size()) {
      ans *= pl[i] % mod;
      ans %= mod;
    }
    rep(i, mi.size()) {
      ans *= mi[i] % mod;
      ans %= mod;
    }
    if (ans < 0)
      ans += mod;
    cout << ans % mod << endl;
    return 0;
  }
  ll i = 0;
  ll j = 0;
  long long ans = 1;
  ll count = 0;
  while (true) {
    if (i + j == k)
      break;
    if (i < pl.size() && (j == mi.size() || pl[i] >= abs(mi[j]))) {
      ans *= pl[i] % mod;
      i++;
      ans %= mod;
    } else {
      ans *= abs(mi[j]) % mod;
      count++;
      j++;
      ans %= mod;
    }
  }
  ans %= mod;
  if (count % 2 == 0)
    cout << ans % mod << endl;
  else {
    if (k != n) {
      if (i != pl.size() && j != mi.size() && j != 0 && i != 0) {
        if (pl[i] * pl[i - 1] > mi[j] * mi[j - 1]) {
          ans = ans * modinv(mi[j - 1] + mod, mod) % mod * pl[i] % mod;
        } else {
          ans = ans * modinv(pl[j - 1], mod) % mod * abs(mi[i]) % mod;
        }
      } else if (i != pl.size() || j != 0) {
        ans = ans * modinv(mi[j - 1] + mod, mod) % mod * pl[i] % mod;
      } else {
        ans = ans * modinv(pl[j - 1], mod) % mod * abs(mi[i]) % mod;
      }
    }
    if (ans < 0)
      ans += mod;
    cout << ans % mod << endl;
  }
}