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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define se second
#define fi first
#define be begin()
#define en end()
#define all(v) v.be, v.en
// template<typename... T> void pp(T... args) { ((cout << args << " "), ...);
// cout<<"\\n"; } template<typename... T> void po(T... args) { ((cout << args <<
// " "), ...); cout<<""; }
#define mod 1000000007

ll gcd(ll a, ll b);

// To compute x raised to power y under modulo m
ll power(ll x, ll y, ll m);

// Function to find modular inverse of a under modulo m
// Assumption: m is prime
ll modInverse(ll a, ll m) {
  ll g = gcd(a, m);
  if (g != 1) {
  }
  // cout << "Inverse doesn't exist";
  else {
    // If a and m are relatively prime, then modulo inverse
    // is a^(m-2) mode m
    // cout << "Modular multiplicative inverse is "
    //      << power(a, m-2, m);
    return power(a, m - 2, m);
  }
}

// To compute x^y under modulo m
ll power(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}

// Function to return gcd of a and b
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int test = 1;
  // cin>>test;
  for (int tt = 1; tt <= test; tt++) {
    ll n, k;
    cin >> n >> k;
    vector<ll> v1, v2, v;
    vector<pair<ll, ll>> vec;
    for (ll i = 0; i < n; i++) {
      ll w;
      cin >> w;
      if (w > 0)
        v1.pb(w);
      else
        v2.pb(-w);
      if (w >= 0) {
        v.pb(w);
        vec.pb({w, 1});
      } else {
        v.pb(-w);
        vec.pb({-w, -1});
      }
    }
    sort(all(v1));
    sort(all(v2));
    sort(all(vec));
    reverse(all(vec));
    reverse(all(v1));
    reverse(all(v2));
    sort(all(v));
    // cout<<k<<" "<<v1.size()<<" ";
    if ((v1.size() == 0 && k % 2 == 1) || (n == k && v2.size() % 2 == 1)) {
      ll ans = 1, cnt = 0;
      for (auto i : v) {
        ans *= i;
        ans %= mod;
        cnt++;
        if (cnt == k)
          break;
      }
      ans = (2 * mod - ans) % mod;
      // cout<<"YES";
      cout << ans << endl;
    } else {
      ll prep = -1, pren = -1, nxtp = -1, nxtn = -1;
      ll cnt = 0;
      ll cntn = 0;
      ll ans = 1;
      for (auto i : vec) {

        if (cnt < k) {
          ans *= i.fi;
          ans %= mod;
          if (i.se == -1) {
            pren = i.fi;
            cntn++;
          } else
            prep = i.fi;
          cnt++;
        } else {
          if (i.se == 1 && nxtp == -1) {
            nxtp = i.fi;
          }
          if (i.se == -1 && nxtn == -1) {
            nxtn = i.fi;
          }
        }
      }
      if (cntn % 2 == 0) {

        cout << ans << endl;
      } else {
        // cout<<pren<<" "<<prep<<endl;
        if (nxtp == -1 || pren == -1) {
          ans *= modInverse(prep, mod);
          ans %= mod;
          ans *= nxtn;
          ans %= mod;
        } else if (nxtn == -1 || prep == -1) {
          ans *= modInverse(pren, mod);
          ans %= mod;
          ans *= nxtp;
          ans %= mod;
        } else if (nxtp * prep > nxtn * pren) {
          ans *= modInverse(pren, mod);
          ans %= mod;
          ans *= nxtp;
          ans %= mod;
        } else {
          ans *= modInverse(prep, mod);
          ans %= mod;
          ans *= nxtn;
          ans %= mod;
        }
        //    ans=(-ans+mod)%mod;
        cout << ans << endl;
      }
    }

    // cout<<"Case #"<<tt<<": ";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define se second
#define fi first
#define be begin()
#define en end()
#define all(v) v.be, v.en
// template<typename... T> void pp(T... args) { ((cout << args << " "), ...);
// cout<<"\\n"; } template<typename... T> void po(T... args) { ((cout << args <<
// " "), ...); cout<<""; }
#define mod 1000000007

ll gcd(ll a, ll b);

// To compute x raised to power y under modulo m
ll power(ll x, ll y, ll m);

// Function to find modular inverse of a under modulo m
// Assumption: m is prime
ll modInverse(ll a, ll m) {
  ll g = gcd(a, m);
  if (g != 1) {
  }
  // cout << "Inverse doesn't exist";
  else {
    // If a and m are relatively prime, then modulo inverse
    // is a^(m-2) mode m
    // cout << "Modular multiplicative inverse is "
    //      << power(a, m-2, m);
    return power(a, m - 2, m);
  }
}

// To compute x^y under modulo m
ll power(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;

  return (y % 2 == 0) ? p : (x * p) % m;
}

// Function to return gcd of a and b
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int test = 1;
  // cin>>test;
  for (int tt = 1; tt <= test; tt++) {
    ll n, k;
    cin >> n >> k;
    vector<ll> v1, v2, v;
    vector<pair<ll, ll>> vec;
    for (ll i = 0; i < n; i++) {
      ll w;
      cin >> w;
      if (w > 0)
        v1.pb(w);
      else
        v2.pb(-w);
      if (w > 0) {
        v.pb(w);
        vec.pb({w, 1});
      } else {
        v.pb(-w);
        vec.pb({-w, -1});
      }
    }
    sort(all(v1));
    sort(all(v2));
    sort(all(vec));
    reverse(all(vec));
    reverse(all(v1));
    reverse(all(v2));
    if ((v1.size() == 0 && k % 2 == 1) || (n == k && v2.size() % 2 == 1)) {
      ll ans = 1, cnt = 0;
      for (auto i : v) {
        ans *= i;
        ans %= mod;
        cnt++;
        if (cnt == k)
          break;
      }
      ans = (mod - ans) % mod;
      cout << ans << endl;
    } else {
      ll prep = -1, pren = -1, nxt = -1;
      ll cnt = 0;
      ll cntn = 0;
      ll ans = 1;
      for (auto i : vec) {

        if (cnt < k) {
          ans *= i.fi;
          ans %= mod;
          if (i.se == -1) {
            pren = i.fi;
            cntn++;
          } else
            prep = i.fi;
          cnt++;
        } else {
          if (i.se == 1 && nxt == -1) {
            nxt = i.fi;
          }
        }
      }
      if (cntn % 2 == 0) {

        cout << ans << endl;
      } else {
        ans *= modInverse(pren, mod);
        ans *= nxt;
        ans %= mod;
        cout << ans << endl;
      }
    }

    // cout<<"Case #"<<tt<<": ";
  }

  return 0;
}