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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007

ll modpow(ll n, ll r) {
  ll res = 1;
  while (r) {
    if (r & 1)
      res = res * n % MOD;
    n = n * n % MOD;
    r >>= 1;
  }
  return res;
}

int main() {

  ll n, K;
  cin >> n >> K;
  vector<pair<ll, ll>> v;
  ll gg = 0, pogi = 0;
  ll zero = 0;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x > 0)
      v.push_back(make_pair(x, 1)), pogi++;
    else if (x < 0)
      v.push_back(make_pair(-x, -1)), gg++;
    else if (x == 0)
      zero++;
  }
  sort(ALL(v), greater<pair<ll, ll>>());
  ll res1 = 1;
  bool sei = true;
  if (pogi + gg < K) {
    cout << 0 << endl;
    return 0;
  }
  ll cnt = 0;
  ll ln = -1, pn = -1;
  rep(i, K) {
    res1 = res1 * v[i].first % MOD;
    if (v[i].second < 0)
      cnt++, ln = v[i].first;
    else
      pn = v[i].first;
  }
  if (cnt & 1) {
    ll t = -1, tt = -1;
    ll res2 = res1 * modpow(ln, MOD - 2) % MOD;
    sei = false;
    for (ll i = K; i < v.size(); i++) {
      if (v[i].second > 0) {
        res2 = res2 * v[i].first % MOD;
        t = v[i].first;
        sei = true;
        break;
      }
    }
    if (!sei)
      res2 = -1;
    bool sei3 = true;
    ll res3 = res1 * modpow(pn, MOD - 2) % MOD;
    sei3 = false;
    for (ll i = K; i < v.size(); i++) {
      if (v[i].second < 0) {
        res3 = res3 * v[i].first % MOD;
        sei3 = true;
        tt = v[i].first;
        break;
      }
    }
    if (pn < 0)
      sei3 = false;
    if (!sei3)
      res3 = -1;
    if (sei && sei3) {
      if (tt * ln < t * pn)
        res1 = res2;
      else
        res1 = res3;
    } else if (sei && !sei3) {
      res1 = res2;
    } else {
      res1 = res3;
    }
    sei = (sei || sei3);
  }
  res1 = (res1 + MOD) % MOD;
  if (sei) {
    cout << res1 << endl;
    return 0;
  }
  ll res = 1;
  sort(ALL(v));
  int jjj = 0;
  rep(i, K) {
    if (v[i].second > 0)
      res = res * v[i].first % MOD;
    else {
      res *= -v[i].first;
      res = (res + MOD) % MOD;
      jjj++;
    }
  }
  res = (res + MOD) % MOD;
  if ((jjj & 1) && zero > 0) {
    cout << 0 << endl;
    return 0;
  }
  cout << res << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007

ll modpow(ll n, ll r) {
  ll res = 1;
  while (r) {
    if (r & 1)
      res = res * n % MOD;
    n = n * n % MOD;
    r >>= 1;
  }
  return res;
}

int main() {

  int n, K;
  cin >> n >> K;
  vector<pair<ll, int>> v;
  ll gg = 0, pogi = 0;
  int zero = 0;
  rep(i, n) {
    ll x;
    cin >> x;
    if (x > 0)
      v.push_back(make_pair(x, 1)), pogi++;
    else if (x < 0)
      v.push_back(make_pair(-x, -1)), gg++;
    else if (x == 0)
      zero++;
  }
  sort(ALL(v), greater<pair<ll, int>>());
  int vv = v.size();
  ll res1 = 1;
  bool sei = true;
  {
    int cnt = 0;
    ll ln = 1;
    rep(i, K) {
      res1 = res1 * v[i].first % MOD;
      if (v[i].second < 0)
        cnt++, ln = v[i].first;
    }
    if (cnt & 1) {
      res1 = res1 * modpow(ln, MOD - 2) % MOD;
      sei = false;
      for (int i = K; i < v.size(); i++) {
        if (v[i].second > 0) {
          res1 = res1 * v[i].first % MOD;
          sei = true;
          break;
        }
      }
    }
  }
  res1 = (res1 + MOD) % MOD;
  if (sei) {
    cout << res1 << endl;
    return 0;
  }
  if (zero > 0) {
    cout << 0 << endl;
    return 0;
  }
  ll res = 1;
  sort(ALL(v));
  rep(i, K) res = res * v[i].first % MOD;
  res = (-res + MOD) % MOD;
  cout << res << endl;

  return 0;
}