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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

constexpr ll mod = 1e9 + 7;
struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}

  friend ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

  friend istream &operator>>(istream &is, mint &a) {
    ll t;
    is >> t;
    a = mint(t);
    return (is);
  }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  vec<ll> P(N);
  bool allminus = true;
  for (int i = 0; i < N; i++) {
    cin >> P[i];
    allminus &= P[i] < 0;
  }
  if (K == N) {
    mint ans = 1;
    for (int i = 0; i < K; i++)
      ans *= P[i];
    cout << ans << "\\n";
    return 0;
  }
  if (allminus && K % 2 == 1) {
    sort(P.begin(), P.end(), [](ll a, ll b) { return abs(a) < abs(b); });
    mint ans = 1;
    for (int i = 0; i < K; i++)
      ans *= P[i];
    cout << ans << "\\n";
    return 0;
  }

  sort(P.begin(), P.end(), [](ll a, ll b) { return abs(a) > abs(b); });
  mint ans = 1;
  if (P[K - 1] == 0) {
    cout << 0 << "\\n";
    return 0;
  }
  vec<ll> plus, minus;
  for (int i = 0; i < K; i++) {
    ans *= P[i];
    if (P[i] > 0)
      plus.push_back(P[i]);
    else if (P[i] < 0)
      minus.push_back(P[i]);
  }
  if (minus.size() % 2 == 0) {
    cout << ans << "\\n";
    return 0;
  }
  ll p = -1, m = 1;
  for (int i = K; i < N; i++) {
    if (p < 0 && P[i] >= 0)
      p = P[i];
    if (m > 0 && P[i] < 0)
      m = P[i];
  }
  if (m > 0 || plus.empty()) {
    ans /= minus.back();
    ans *= p;
    cout << ans << "\\n";
  } else if (p < 0 || minus.empty()) {
    ans /= plus.back();
    ans *= m;
    cout << ans << "\\n";
  } else {
    if (abs(p * plus.back()) < abs(m * minus.back())) {
      ans /= plus.back();
      ans *= m;
    } else {
      ans /= minus.back();
      ans *= p;
    }
    cout << ans << "\\n";
  }
}


Error: 0

Fixed submission:

// AUTHOR: RAVAN_2070
// PUNE INSTITUTE OF COMPUTER TECHNOLOGY
/*
      I ♥ CLARICE STARLING...
        EXPLAINATION BELOW->
*/
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vii;
typedef pair<ll, ll> pii;
typedef map<ll, ll> mii;
#define MOD7 1000000007
#define MOD9 1000000009
#define pi 3.1415926535
#define Test_cases                                                             \\
  ll TC;                                                                       \\
  cin >> TC;                                                                   \\
  while (TC--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sp(x) fixed << setprecision(x)
#define sz(x) (ll)(x.size())
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
const ll INF = (ll)2e18 + 77;
bool compare(ll a, ll b) {
  if (abs(a) > abs(b))
    return true;
  else if (abs(a) < abs(b))
    return false;
  else
    return (a > 0 ? true : false);
}
void solve() {
  ll n, i, k, N = 0, P = 0, ne = 0, po = 0, z = 0;
  cin >> n >> k;
  vector<ll> a(n);
  fo(i, 0, n) {
    cin >> a[i];
    if (a[i] < 0)
      N++;
  }
  P = n - N;
  sort(all(a), compare);

  ll ans, pro;
  ans = pro = 1;
  if (N == n) {
    for (i = n - 1; i >= 0; i--) {
      k--;
      ans *= a[i];
      ans %= MOD7;
      if (ans < 0)
        ans += MOD7;
      if (k == 0)
        break;
    }
    cout << ans << "\\n";
    return;
  } else {
    fo(i, 0, k) {
      if (a[i] < 0)
        ne++;
      if (a[i] == 0)
        z++;
    }
    if (z > 0) {
      cout << 0 << "\\n";
      return;
    }
    double w, x, y, z;
    if (ne & 1) {
      if (N != ne) {
        for (i = k; i < n; i++)
          if (a[i] < 0)
            break;

        w = a[i];
        ans = ans * a[i];
        ans %= MOD7;
        if (ans < 0)
          ans += MOD7;

        int f = 0;
        for (i = k - 1; i >= 0; i--) {
          if (f == 0 && a[i] >= 0)
            f = 1, x = a[i];
          ans = (ans * a[i]) % MOD7;
          if (ans < 0)
            ans += MOD7;
        }
      }
      po = k - ne;
      if (P != po) {
        for (i = k; i < n; i++)
          if (a[i] >= 0)
            break;
        y = a[i];
        pro = (pro * a[i]) % MOD7;
        int f = 0;
        for (i = k - 1; i >= 0; i--) {
          if (f == 0 && a[i] < 0)
            f = 1, z = a[i];
          pro = (pro * a[i]) % MOD7;
          if (pro < 0)
            pro += MOD7;
        }
      }

      if (N != ne && P != po) {
        w = abs(w);
        x = abs(x);
        y = abs(y);
        z = abs(z);
        if (w / x > y / z)
          cout << ans << "\\n";
        else
          cout << pro << "\\n";
        return;
      }
      if (N == ne)
        ans = pro;
    } else {
      fo(i, 0, k) {
        ans = ans * a[i];
        ans %= MOD7;
        if (ans < 0)
          ans += MOD7;
      }
    }
    cout << ans << "\\n";
    return;
  }
}
int main() {
  fastio
  // Test_cases
  solve();
  return 0;
}
// map<ll,ll,greater<int>> to sort in descending order ,in iterator declaration
// no greater<int> if you want to apply lowerbound on set use
// s.lower_bound(key),not the one used for vector
//  Syntax to create a min heap for priority queue priority_queue <int,
//  vector<int>, greater<int>>;
// for modinverse use modularexponention(x,M-2,M);
// for "each" subtraction from ans(ans-k) if(ans<0) do ans+=MOD,ans%=MOD
// vector<vector<int>>dp(201,vector<int>(n+1)); A way to initialize vector of
// vector If wrong on pretest 2 means logically wrong

/* TIPS FOR DEBUGGING
 * Check if values are not overflowing (use long long where required)
 * Check if ranges are inclusive or not.
 * Check properly if greedy will work or not before using it.
 * Check EDGE CASES!! Seriously, even in 2B, saving 8 points by being
 * 2 minutes faster will bite you in the ass if you don't make sure
 * it works on edge cases.
 * Keep adding to this list.
 */