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

#include "bits/stdc++.h"

using namespace std;
using ll = long long;

const ll MOD = 1e9 + 7;

inline void normal(ll &a) {
  if (a >= MOD)
    a %= MOD;
  (a < 0) && (a += MOD);
}
inline ll modMul(ll a, ll b) {
  normal(a), normal(b);
  return (a * b) % MOD;
}
inline ll modAdd(ll a, ll b) {
  normal(a), normal(b);
  return (a + b) % MOD;
}
inline ll modSub(ll a, ll b) {
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
} // if MOD is prime, modPow(b, p % (MOD-1))
inline ll modInverse(ll a) { return modPow(a, MOD - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  vector<ll> pos, neg;

  int z = 0;
  for (int i = 0; i < n; i++) {
    ll x;
    scanf("%lld", &x);
    if (x > 0)
      pos.push_back(x);
    if (x < 0)
      neg.push_back(x);
    if (x == 0)
      z++;
  }

  sort(pos.begin(), pos.end(), greater<ll>());
  sort(neg.begin(), neg.end());

  ll ans = 1;
  int ip = 0, jn = 0;

  //    int psz = pos.size(), nsz = neg.size();
  if (k & 1) {
    //        psz--;
    if (pos.empty()) {
      if (z > 0) {
        cout << 0 << endl;
        return 0;
      } else {
        reverse(neg.begin(), neg.end());
        for (int i = 0; i < k; i++)
          ans = modMul(ans, neg[i]);
        cout << ans << endl;
        return 0;
      }
    } else {
      ans = modMul(ans, pos[0]);
      ip++;
      k--;
    }
  }

  while (k > 0) {
    if (ip + 1 >= pos.size() and jn + 1 >= neg.size()) {
      if (ip < pos.size()) {
        ans = modMul(ans, pos[ip]);
        ip++;
        k--;
        continue;
      }

      if (z > 0) {
        cout << 0 << endl;
        return 0;
      }

      if (jn < neg.size()) {
        ans = modMul(ans, neg[jn]);
        jn++;
        k--;
        continue;
      }
    }

    if (ip + 1 >= pos.size()) {
      ll negmul = neg[jn] * neg[jn + 1];
      jn += 2;
      k -= 2;
      ans = modMul(ans, negmul);
      continue;
    }

    if (jn + 1 >= neg.size()) {
      ll posmul = pos[ip] * pos[ip + 1];
      ip += 2;
      k -= 2;
      ans = modMul(ans, posmul);
      continue;
    }

    ll posmul = pos[ip] * pos[ip + 1];
    ll negmul = neg[jn] * neg[jn + 1];

    if (posmul > negmul) {
      ans = modMul(ans, posmul);
      ip += 2;
      k -= 2;
    } else {
      ans = modMul(ans, negmul);
      jn += 2;
      k -= 2;
    }
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;
using ll = long long;

const ll MOD = 1e9 + 7;

inline void normal(ll &a) {
  if (a >= MOD)
    a %= MOD;
  (a < 0) && (a += MOD);
}
inline ll modMul(ll a, ll b) {
  normal(a), normal(b);
  return (a * b) % MOD;
}
inline ll modAdd(ll a, ll b) {
  normal(a), normal(b);
  return (a + b) % MOD;
}
inline ll modSub(ll a, ll b) {
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline ll modPow(ll b, ll p) {
  ll r = 1;
  while (p) {
    if (p & 1)
      r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
} // if MOD is prime, modPow(b, p % (MOD-1))
inline ll modInverse(ll a) { return modPow(a, MOD - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  vector<ll> pos, neg;

  int z = 0;
  for (int i = 0; i < n; i++) {
    ll x;
    scanf("%lld", &x);
    if (x > 0)
      pos.push_back(x);
    if (x < 0)
      neg.push_back(x);
    if (x == 0)
      z++;
  }

  sort(pos.begin(), pos.end(), greater<ll>());
  sort(neg.begin(), neg.end());

  ll ans = 1;
  int ip = 0, jn = 0;

  //    int psz = pos.size(), nsz = neg.size();
  if (k & 1) {
    //        psz--;
    if (pos.empty()) {
      if (z > 0) {
        cout << 0 << endl;
        return 0;
      } else {
        int sz = neg.size();
        for (int i = sz - 1, j = 0; j < k; i--, j++) {
          ans = modMul(ans, neg[i]);
        }

        cout << ans << endl;
        return 0;
      }
    } else {
      ans *= pos[0];
      ip++;
      k--;
    }
  }

  while (k > 0) {
    if (ip + 1 >= pos.size() and jn + 1 >= neg.size()) {
      if (ip <= pos.size()) {
        ans = modMul(ans, pos[ip]);
        ip++;
        k--;
        continue;
      }

      if (z > 0) {
        cout << 0 << endl;
        return 0;
      }

      if (jn <= neg.size()) {
        ans = modMul(ans, neg[jn]);
        jn++;
        k--;
        continue;
      }
    }

    if (ip + 1 >= pos.size()) {
      ll negmul = neg[jn] * neg[jn + 1];
      jn += 2;
      k -= 2;
      ans = modMul(ans, negmul);
      continue;
    }

    if (jn + 1 >= neg.size()) {
      ll posmul = pos[ip] * pos[ip + 1];
      ip += 2;
      k -= 2;
      ans = modMul(ans, posmul);
      continue;
    }

    ll posmul = pos[ip] * pos[ip + 1];
    ll negmul = neg[jn] * neg[jn + 1];

    if (posmul > negmul) {
      ans = modMul(ans, posmul);
      ip += 2;
      k -= 2;
    } else {
      ans = modMul(ans, negmul);
      jn += 2;
      k -= 2;
    }
  }

  cout << ans << endl;
}