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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

long long modpow(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = (ret * x) % MOD;
    x = (x * x) % MOD;
    n >>= 1;
  }
  return ret;
}

long long inverse(long long x) { return modpow(x, MOD - 2); }

int main() {
  int n, k;
  cin >> n >> k;
  vector<pll> a;
  int mm = 0, zz = 0;
  rep(i, n) {
    ll b;
    cin >> b;
    if (b > 0)
      a.push_back(pll(b, 1));
    else if (b < 0) {
      a.push_back(pll(-b, -1));
      ++mm;
    } else {
      ++zz;
      a.push_back(pll(b, 0));
    }
  }
  sort(a.begin(), a.end(), greater<pll>());
  if (a[k - 1].first == 0) {
    cout << 0 << endl;
    return 0;
  }

  if (k % 2 == 1 && mm == n) {
    ll ans = 1;
    rep(i, n) a[i].first = -a[i].first + MOD;
    rep(i, k) ans = (ans * a[n - i - 1].first) % MOD;
    cout << ans << endl;
    return 0;
  }

  if (mm + zz == n) {
    if (k % 2 == 1) {
      cout << 0 << endl;
      return 0;
    } else {
      ll ans = 1;
      rep(i, k) ans = (ans * a[i].first) % MOD;
      cout << ans << endl;
      return 0;
    }
  }

  if (k == n) {
    ll ans = 1;
    rep(i, n) {
      if (a[i].second < 0)
        a[i].first = -a[i].first + MOD;
    }
    rep(i, n) ans = (ans * a[i].first) % MOD;
    cout << ans << endl;
    return 0;
  }

  if (a[k].first == 0) {
    if (mm % 2 == 1 && k % 2 == 1) {
      cout << 0 << endl;
      return 0;
    } else {
      ll ans = 1;
      rep(i, n) {
        if (a[i].second < 0)
          a[i].first = -a[i].first + MOD;
      }
      rep(i, k) ans = (ans * a[i].first) % MOD;
      cout << ans << endl;
      return 0;
    }
  }

  ll ans = 1;
  int c = 0;
  rep(i, k) {
    ans = (ans * a[i].first) % MOD;
    if (a[i].second < 0)
      ++c;
  }

  if (c % 2 == 0) {
    cout << ans << endl;
    return 0;
  }

  ll im = 0, ip = 0;

  for (int i = k - 1; i >= 0; i--) {
    if (a[i].second < 0 && im == 0)
      im = a[i].first;
    if (a[i].second > 0 && ip == 0)
      ip = a[i].first;
  }

  ll m = 0, p = 0;

  for (int i = k; i < n; i++) {
    if (a[i].second < 0 && m == 0)
      m = a[i].first;
    if (a[i].second > 0 && p == 0)
      p = a[i].first;
  }

  /*
  cout << ans << endl;
  cout << inm << " " << outp << endl;
  cout << inp << " " << outm << endl;
  */

  if (p != 0 && m != 0 && ip != 0 && im != 0) {
    if (abs(m * im) > abs(p * ip)) {
      ans = (((ans * inverse(ip)) % MOD) * (m + MOD) % MOD) % MOD;
      cout << ans << endl;
    } else {
      ans = (((ans * inverse((im + MOD) % MOD)) % MOD) * p) % MOD;
      cout << ans << endl;
    }
  } else if (im != 0 && p != 0) {
    ans = (((ans * inverse((im + MOD) % MOD)) % MOD) * p) % MOD;
    cout << ans << endl;
  } else if (ip != 0 && m != 0) {
    ans = (((ans * inverse(ip)) % MOD) * (m + MOD) % MOD) % MOD;
    cout << ans << endl;
  } else {
    cout << 0 << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

long long modpow(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = (ret * x) % MOD;
    x = (x * x) % MOD;
    n >>= 1;
  }
  return ret;
}

long long inverse(long long x) { return modpow(x, MOD - 2); }

int main() {
  int n, k;
  cin >> n >> k;
  vector<pll> a;
  int mm = 0, zz = 0;
  rep(i, n) {
    ll b;
    cin >> b;
    if (b > 0)
      a.push_back(pll(b, 1));
    else if (b < 0) {
      a.push_back(pll(-b, -1));
      ++mm;
    } else {
      ++zz;
    }
  }
  sort(a.begin(), a.end(), greater<pll>());
  if (a[k - 1].first == 0) {
    cout << 0 << endl;
    return 0;
  }

  if (k % 2 == 1 && mm == n) {
    ll ans = 1;
    rep(i, n) a[i].first = -a[i].first + MOD;
    rep(i, k) ans = (ans * a[n - i - 1].first) % MOD;
    cout << ans << endl;
    return 0;
  }

  if (mm + zz == n) {
    if (k % 2 == 1) {
      cout << 0 << endl;
      return 0;
    } else {
      ll ans = 1;
      rep(i, k) ans = (ans * a[i].first) % MOD;
      cout << ans << endl;
      return 0;
    }
  }

  if (k == n) {
    ll ans = 1;
    rep(i, n) {
      if (a[i].second < 0)
        a[i].first = -a[i].first + MOD;
    }
    rep(i, n) ans = (ans * a[i].first) % MOD;
    cout << ans << endl;
    return 0;
  }

  if (a[k].first == 0) {
    if (mm % 2 == 1) {
      cout << 0 << endl;
      return 0;
    } else {
      ll ans = 1;
      rep(i, n) {
        if (a[i].second < 0)
          a[i].first = -a[i].first + MOD;
      }
      rep(i, k) ans = (ans * a[i].first) % MOD;
      cout << ans << endl;
      return 0;
    }
  }

  ll ans = 1;
  int c = 0;
  rep(i, k) {
    ans = (ans * a[i].first) % MOD;
    if (a[i].second < 0)
      ++c;
  }

  if (c % 2 == 0) {
    cout << ans << endl;
    return 0;
  }

  ll inm = 0, inp = 0;

  for (int i = k - 1; i >= 0; i--) {
    if (a[i].second < 0 && inm != 0)
      inm = a[i].first;
    if (a[i].second > 0 && inp != 0)
      inp = a[i].first;
  }

  ll outm = 0, outp = 0;

  for (int i = k; i < n; i++) {
    if (a[i].second < 0 && outm != 0)
      outm = a[i].first;
    if (a[i].second > 0 && outp != 0)
      outp = a[i].first;
  }

  ll p = 0, q = 0;
  if (inm != 0 && outp != 0)
    p = ((ans * inverse(inm)) % MOD * outp) % MOD;
  if (inp != 0 && outm != 0)
    q = ((ans * inverse(inp)) % MOD * outm) % MOD;

  ll anss = 0;
  if (p != 0)
    anss = p;
  if (q != 0)
    anss = max(p, q);

  cout << anss << endl;

  return 0;
}