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

#include <bits/stdc++.h>
#define INF 5000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

//=============modinv============================
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll 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() {
  ll mod = 1e9 + 7;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, K;
  cin >> N >> K;
  vector<pll> A(N); // 0 neg, 1 pos
  for (ll i = 0; i < N; ++i) {
    ll a;
    cin >> a;
    if (a < 0) {
      A.at(i).second = 0;
    } else {
      A.at(i).second = 1;
    }
    A.at(i).first = abs(a);
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  bool p1_use = false, n1_use = false;
  ll p1 = 0, n1 = 0, p2 = 0, n2 = 0;
  for (ll i = 0; i < K; ++i) {
    if (A.at(i).second == 0) {
      n1 = A.at(i).first;
      n1_use = true;
    } else {
      p1 = A.at(i).first;
      p1_use = true;
    }
  }
  for (ll i = N - 1; i >= K; --i) {
    if (A.at(i).second == 0) {
      n2 = A.at(i).first;
    } else {
      p2 = A.at(i).first;
    }
  }
  ll ans = 1;
  ll cnt = 0;
  for (ll i = 0; i < K; ++i) {
    ans = (ans * A.at(i).first) % mod;
    if (A.at(i).second == 0) {
      cnt += 1;
    }
  }
  if (cnt % 2 == 0) {
    cout << ans << "\\n";
    return 0;
  }
  if ((p1 * n2 == 0) && (n1 * p2 == 0)) {
    ans = 1;
    for (ll i = N - 1; i >= N - K; --i) {
      ans = (ans * A.at(i).first) % mod;
    }
    ans = mod - ans;
    ans %= mod;
    cout << ans << "\\n";
    return 0;
  }
  if (n1_use && p1_use) {
    if (n1 * n2 < p1 * p2) {
      ans = ((ans * modinv(n1, mod)) % mod) * p2 % mod;
    } else {
      ans = ((ans * modinv(p1, mod)) % mod) * n2 % mod;
    }
  } else if (p1_use && (!n1_use)) {
    ans = ((ans * modinv(p1, mod)) % mod) * n2 % mod;
  } else {
    ans = ((ans * modinv(n1, mod)) % mod) * p2 % mod;
  }
  cout << ans << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 5000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

//=============modinv============================
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll 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() {
  ll mod = 1e9 + 7;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, K;
  cin >> N >> K;
  vector<pll> A(N); // 0 neg, 1 pos
  for (ll i = 0; i < N; ++i) {
    ll a;
    cin >> a;
    if (a < 0) {
      A.at(i).second = 0;
    } else {
      A.at(i).second = 1;
    }
    A.at(i).first = abs(a);
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  ll p1 = 0, n1 = 0, p2 = 0, n2 = 0;
  for (ll i = 0; i < K; ++i) {
    if (A.at(i).second == 0) {
      n1 = A.at(i).first;
    } else {
      p1 = A.at(i).first;
    }
  }
  for (ll i = N - 1; i >= K; --i) {
    if (A.at(i).second == 0) {
      n2 = A.at(i).first;
    } else {
      p2 = A.at(i).first;
    }
  }
  ll ans = 1;
  ll cnt = 0;
  for (ll i = 0; i < K; ++i) {
    ans = (ans * A.at(i).first) % mod;
    if (A.at(i).second == 0) {
      cnt += 1;
    }
  }
  if (cnt % 2 == 0) {
    cout << ans << "\\n";
    return 0;
  }
  cnt = 0;
  for (ll i = 0; i < N; ++i) {
    if (A.at(i).second == 0) {
      cnt += 1;
    }
  }
  if ((p1 * n2 == 0) && (n1 * p2 == 0)) {
    ans = 1;
    for (ll i = N - 1; i >= N - K; --i) {
      ans = (ans * A.at(i).first) % mod;
    }
    ans = mod - ans;
    ans %= mod;
    cout << ans << "\\n";
    return ans;
  }
  if (p1 * p2 > n1 * n2) {
    ans = (ans * modinv(n1, mod)) % mod * p2 % mod;
  } else if (p1 * p2 <= n1 * n2) {
    ans = (ans * modinv(p1, mod)) % mod * n2 % mod;
  }
  cout << ans << "\\n";
}