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

#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL << 60;
const long long int Mod = 1000000007;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define rp(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
  if (former < b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \\
  std::sort(v.begin(), v.end(),                                                \\
            std::greater<decltype(v[0])>()) // 降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v>>
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\\n"; };
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////

ll pow(ll x, ll y, ll mod) {
  if (y == 0)
    return 1;
  if (y & 1)
    return x * pow(x, y - 1, mod) % mod;
  ll p = pow(x, y / 2, mod);
  return p * p % mod;
}

void solve() {
  ll n, k;
  cin >> n >> k;
  Vi a(n);
  Vi plus, zero, minus;
  ll pnum = 0, znum = 0, mnum = 0;
  rp(i, n) {
    cin >> a.at(i);
    if (a[i] > 0) {
      plus.pb(a[i]);
      pnum++;
    } else if (a[i] == 0) {
      zero.pb(a[i]);
      znum++;
    } else {
      minus.pb(a[i]);
      mnum++;
    }
  }
  if (n == k) {
    ll an = 1;
    rp(i, n) {
      if (a[i] < 0)
        a[i] += Mod;
      an *= a[i];
      an %= Mod;
    }
    print(an);
    return;
  }
  if (k > pnum + mnum) {
    print(0);
    return;
  }
  if (k == pnum + mnum && mnum % 2 == 1) {
    print(0);
    return;
  }
  if (pnum == 0 && k % 2 == 1 && znum > 0) {
    print(0);
    return;
  }
  sort(all(minus));
  Sort(plus);
  if (pnum == 0 && znum == 0) {
    ll an = 1;
    if (k & 1) {
      rp(i, k) {
        an *= -minus[n - i - 1];
        an %= Mod;
      }
      print(Mod - an);
    } else {
      rp(i, k) {
        an *= -minus[i];
        an %= Mod;
      }
      print(an);
    }

    return;
  }
  ll ans = 1;
  ll ctr = 0;
  ll po = 0, ne = 0;
  while (ctr < k) {
    if (po != pnum && (ne == mnum || plus[po] >= -minus[ne])) {
      ans *= plus[po];
      ans %= Mod;
      po++;
      ctr++;
    } else {
      ans *= -minus[ne];
      ans %= Mod;
      ne++;
      ctr++;
    }
  }
  // print(ans);
  if (ne % 2 == 0) {
    print(ans);
  } else {
    if (po == pnum) {
      ans *= pow(plus[--po], Mod - 2, Mod);
      ans %= Mod;
      ans *= -minus[ne];
      ans %= Mod;
      print(ans);
      return;
    } else if (ne == mnum || po == 0) {
      ans *= pow(-minus[--ne], Mod - 2, Mod);
      ans %= Mod;
      ans *= plus[po];
      ans %= Mod;
      print(ans);
      return;
    } else {
      if (minus[ne] * minus[ne - 1] > plus[po] * plus[po - 1]) {
        ans *= pow(plus[--po], Mod - 2, Mod);
        ans %= Mod;
        ans *= -minus[ne];
        ans %= Mod;
        print(ans);
        return;
      } else {
        ans *= pow(-minus[--ne], Mod - 2, Mod);
        ans %= Mod;
        ans *= plus[po];
        ans %= Mod;
        print(ans);
        return;
      }
    }
  }
  return;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(30);
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL << 60;
const long long int Mod = 1000000007;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define rp(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
  if (former < b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \\
  std::sort(v.begin(), v.end(),                                                \\
            std::greater<decltype(v[0])>()) // 降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v>>
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\\n"; };
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////

ll pow(ll x, ll y, ll mod) {
  if (y == 0)
    return 1;
  if (y & 1)
    return x * pow(x, y - 1, mod) % mod;
  ll p = pow(x, y / 2, mod);
  return p * p % mod;
}

void solve() {
  ll n, k;
  cin >> n >> k;
  Vi a(n);
  Vi plus, zero, minus;
  ll pnum = 0, znum = 0, mnum = 0;
  rp(i, n) {
    cin >> a.at(i);
    if (a[i] > 0) {
      plus.pb(a[i]);
      pnum++;
    } else if (a[i] == 0) {
      zero.pb(a[i]);
      znum++;
    } else {
      minus.pb(a[i]);
      mnum++;
    }
  }
  if (n == k) {
    ll an = 1;
    rp(i, n) {
      an *= a[i];
      an %= Mod;
    }
    print(an);
    return;
  }
  if (k > pnum + mnum) {
    print(0);
    return;
  }
  if (k == pnum + mnum && mnum % 2 == 1) {
    print(0);
    return;
  }
  if (pnum == 0 && k % 2 == 1 && znum > 0) {
    print(0);
    return;
  }
  sort(all(plus));
  Sort(minus);
  if (pnum == 0 && znum == 0) {
    ll an = 1;
    rp(i, k) {
      an *= -minus[i];
      an %= Mod;
    }
    print(Mod - an);
    return;
  }
  ll ans = 1;
  ll ctr = 0;
  ll po = 0, ne = 0;
  while (ctr < k) {
    if (plus[po] >= -minus[ne]) {
      ans *= plus[po];
      ans %= Mod;
      po++;
      ctr++;
    } else {
      ans *= -minus[ne];
      ans %= Mod;
      ne++;
      ctr++;
    }
  }
  if (ne % 2 == 0) {
    print(ans);
  } else {
    if (po == pnum) {
      ans *= pow(plus[--po], Mod - 2, Mod);
      ans %= Mod;
      ans *= -minus[ne];
      ans %= Mod;
      print(ans);
      return;
    } else if (ne == mnum) {
      ans *= pow(-minus[--ne], Mod - 2, Mod);
      ans %= Mod;
      ans *= plus[po];
      ans %= Mod;
      print(ans);
      return;
    } else {
      if (minus[ne] * minus[ne - 1] > plus[po] * plus[po - 1]) {
        ans *= pow(plus[--po], Mod - 2, Mod);
        ans %= Mod;
        ans *= -minus[ne];
        ans %= Mod;
        print(ans);
        return;
      } else {
        ans *= pow(-minus[--ne], Mod - 2, Mod);
        ans %= Mod;
        ans *= plus[po];
        ans %= Mod;
        print(ans);
        return;
      }
    }
  }
  return;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(30);
  solve();
  return 0;
}