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

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007ll
using namespace std;
int ara[200005];
ll bigmod(ll b, ll p) {
  if (!p)
    return 1ll;
  ll ret = bigmod(b, p >> 1);
  ret = (ret * ret) % mod;
  if (p & 1ll)
    ret = (ret * b) % mod;
  return ret;
}
int main() {
  int n, k, res = 1;
  scanf("%d %d", &n, &k);
  int neg = 0, z = 0;
  for (int i = 1; i <= n; i++)
    scanf("%d", &ara[i]), z += (ara[i] == 0), neg += (ara[i] < 0);
  if (k == n || (neg == n && k & 1)) {
    sort(ara + 1, ara + 1 + n, greater<int>());
    for (int i = 1; i <= k; i++)
      res = (1LL * res * ara[i]) % mod;
    if (res < 0)
      res += mod;
    printf("%d\\n", res);
  } else if ((n - z) < k || (z && (n - z) == k && (neg & 1)) ||
             (z && !(n - z - neg) && (k & 1)))
    puts("0");
  else {
    vector<int> vp, vn;
    for (int i = 1; i <= n; i++)
      if (ara[i] != 0) {
        if (ara[i] > 0)
          vp.push_back(ara[i]);
        else
          vn.push_back(-ara[i]);
      }
    sort(vp.begin(), vp.end());
    sort(vn.begin(), vn.end());
    int lp = 0, ln = 0, c = 0;
    for (int i = 1; i <= k; i++) {
      if (!vp.empty() && (vn.empty() || vp.back() > vn.back()))
        lp = vp.back(), vp.pop_back(), res = (1LL * res * lp) % mod;
      else
        ln = vn.back(), vn.pop_back(), res = (1LL * res * ln) % mod, c++;
    }
    if (c & 1) {
      if (!vp.empty() && !vn.empty()) {
        if (!lp)
          res =
              ((1LL * res * bigmod(ln, mod - 2)) % mod * 1LL * vp.back()) % mod;
        else if ((1LL * ln * vn.back()) > (1LL * lp * vp.back()))
          res =
              ((1LL * res * bigmod(lp, mod - 2)) % mod * 1LL * vn.back()) % mod;
        else
          res =
              ((1LL * res * bigmod(ln, mod - 2)) % mod * 1LL * vp.back()) % mod;
      } else {
        if (vp.empty() && !vn.empty() && lp)
          res =
              ((1LL * res * bigmod(lp, mod - 2)) % mod * 1LL * vn.back()) % mod;
        else if (!vp.empty() && ln)
          res =
              ((1LL * res * bigmod(ln, mod - 2)) % mod * 1LL * vp.back()) % mod;
      }
      if (res < 0)
        res += mod;
    }
    printf("%d\\n", res);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007ll
using namespace std;
int ara[200005];
ll bigmod(ll b, ll p) {
  if (!p)
    return 1ll;
  ll ret = bigmod(b, p >> 1);
  ret = (ret * ret) % mod;
  if (p & 1ll)
    ret = (ret * b) % mod;
  return ret;
}
int main() {
  int n, k, res = 1;
  scanf("%d %d", &n, &k);
  int neg = 0, z = 0;
  for (int i = 1; i <= n; i++)
    scanf("%d", &ara[i]), z += (ara[i] == 0), neg += (ara[i] < 0);
  if (k == n || neg == n) {
    sort(ara + 1, ara + 1 + n, greater<int>());
    for (int i = 1; i <= k; i++)
      res = (1LL * res * ara[i]) % mod;
    if (res < 0)
      res += mod;
    printf("%d\\n", res);
  } else if ((n - z) < k || ((z + neg) == n && k & 1) ||
             ((n - z) == k && (neg & 1)))
    puts("0");
  else {
    vector<int> vp, vn;
    for (int i = 1; i <= n; i++)
      if (ara[i] != 0) {
        if (ara[i] > 0)
          vp.push_back(ara[i]);
        else
          vn.push_back(-ara[i]);
      }
    sort(vp.begin(), vp.end());
    sort(vn.begin(), vn.end());
    int lp = 0, ln = 0, c = 0;
    for (int i = 1; i <= k; i++) {
      if (!vp.empty() && vp.back() > vn.back())
        lp = vp.back(), vp.pop_back(), res = (1LL * res * lp) % mod;
      else
        ln = vn.back(), vn.pop_back(), res = (1LL * res * ln) % mod, c++;
    }
    if (c & 1) {
      if (!vp.empty() && !vn.empty()) {
        if (!lp)
          res =
              ((1LL * res * bigmod(ln, mod - 2)) % mod * 1LL * vp.back()) % mod;
        else if ((1LL * ln * vn.back()) > (1LL * lp * vp.back()))
          res =
              ((1LL * res * bigmod(lp, mod - 2)) % mod * 1LL * vn.back()) % mod;
        else
          res =
              ((1LL * res * bigmod(ln, mod - 2)) % mod * 1LL * vp.back()) % mod;
      } else {
        if (vp.empty() && !vn.empty() && lp)
          res =
              ((1LL * res * bigmod(lp, mod - 2)) % mod * 1LL * vn.back()) % mod;
        else if (!vp.empty() && ln)
          res =
              ((1LL * res * bigmod(ln, mod - 2)) % mod * 1LL * vp.back()) % mod;
      }
      if (res < 0)
        res += mod;
    }
    printf("%d\\n", res);
  }
  return 0;
}