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

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const int MOD = 1e9 + 7;
int mul(int x, int y) { return (x * 1ll * y) % MOD; }
int bp(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = mul(ans, x);
    x = mul(x, x);
    y >>= 1;
  }
  return ans;
}
int inv(int x) { return bp(x, MOD - 2); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> pos, neg;
  int nonzero = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x)
      nonzero++;
    if (x < 0)
      neg.push_back(-x);
    else
      pos.push_back(x);
  }
  if (nonzero < k) {
    cout << 0;
    return 0;
  }

  int np = pos.size(), nn = neg.size();
  sort(pos.begin(), pos.end(), greater<int>());
  sort(neg.begin(), neg.end(), greater<int>());
  int idp = 0, idn = 0;

  if (n == k) {
    bool negative = (nn & 1);
    int ans = 1;
    for (int i = 0; i < np; i++)
      ans = mul(ans, pos[i]);
    for (int i = 0; i < nn; i++)
      ans = mul(ans, neg[i]);
    if (negative)
      ans = (ans - MOD) % MOD;
    cout << abs(ans) << '\\n';
  } else if (k % 2 == 0) {
    int ans = 1;
    while (idp + idn < k && idp < np && idn < nn) {
      if (pos[idp] >= neg[idn]) {
        ans = mul(ans, pos[idp++]);
      } else
        ans = mul(ans, neg[idn++]);
    }
    while (idp + idn < k && idp < np) {
      ans = mul(ans, pos[idp++]);
    }
    while (idp + idn < k && idn < nn) {
      ans = mul(ans, neg[idn++]);
    }
    if (idn & 1) {
      if (idn < nn && 0 < idp && idp < np) {
        ll takep = pos[idp - 1] * pos[idp];
        ll taken = neg[idn - 1] * neg[idn];
        if (takep >= taken) {
          ans = mul(mul(ans, inv(neg[idn - 1])), pos[idp]);
        } else {
          ans = mul(mul(ans, inv(pos[idp - 1])), neg[idn]);
        }
      } else if (idn == nn || idp == 0) {
        ans = mul(mul(ans, inv(neg[idn - 1])), pos[idp]);
      } else {
        ans = mul(mul(ans, inv(pos[idp - 1])), neg[idn]);
      }
    }
    cout << ans << '\\n';
  } else if (np) {
    int ans = 1;
    while (idp + idn < k && idp < np && idn < nn) {
      if (pos[idp] >= neg[idn]) {
        ans = mul(ans, pos[idp++]);
      } else
        ans = mul(ans, neg[idn++]);
    }
    while (idp + idn < k && idp < np) {
      ans = mul(ans, pos[idp++]);
    }
    while (idp + idn < k && idn < nn) {
      ans = mul(ans, neg[idn++]);
    }
    if (idn & 1) {
      if (idn < nn && 0 < idp && idp < np) {
        ll takep = pos[idp - 1] * pos[idp];
        ll taken = neg[idn - 1] * neg[idn];
        if (takep >= taken) {
          ans = mul(mul(ans, inv(neg[idn - 1])), pos[idp]);
        } else {
          ans = mul(mul(ans, inv(pos[idp - 1])), neg[idn]);
        }
      } else if (idn == nn || idp == 0) {
        ans = mul(mul(ans, inv(neg[idn - 1])), pos[idp]);
      } else {
        ans = mul(mul(ans, inv(pos[idp - 1])), neg[idn]);
      }
    }
    cout << ans << '\\n';
  } else {
    reverse(neg.begin(), neg.end());
    int ans = 1;
    for (int i = 0; i < k; i++)
      ans = mul(ans, neg[i]);
    ans = (ans - MOD) % MOD;
    cout << abs(ans) << '\\n';
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const int MOD = 1e9 + 7;
int mul(int x, int y) { return (x * 1ll * y) % MOD; }
int bp(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = mul(ans, x);
    x = mul(x, x);
    y >>= 1;
  }
  return ans;
}
int inv(int x) { return bp(x, MOD - 2); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> pos, neg;
  int nonzero = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x)
      nonzero++;
    if (x < 0)
      neg.push_back(-x);
    else
      pos.push_back(x);
  }
  if (nonzero < k) {
    cout << 0;
    return 0;
  }
  int np = pos.size(), nn = neg.size();
  sort(pos.begin(), pos.end(), greater<int>());
  sort(neg.begin(), neg.end(), greater<int>());
  int idp = 0, idn = 0;
  if (n == k) {
    bool negative = (nn & 1);
    int ans = 1;
    for (int i = 0; i < np; i++)
      ans = mul(ans, pos[i]);
    for (int i = 0; i < nn; i++)
      ans = mul(ans, neg[i]);
    if (negative)
      ans = (ans - MOD) % MOD;
    cout << abs(ans) << '\\n';
  } else if (k % 2 == 0) {
    int ans = 1;
    while (idp + idn < k && idp < np && idn < nn) {
      if (pos[idp] >= neg[idn]) {
        ans = mul(ans, pos[idp++]);
      } else
        ans = mul(ans, neg[idn++]);
    }
    while (idp + idn < k && idp < np) {
      ans = mul(ans, pos[idp++]);
    }
    while (idp + idn < k && idn < nn) {
      ans = mul(ans, pos[idn++]);
    }
    if (idn & 1) {
      if (idn < nn && 0 < idp && idp < np) {
        ll takep = pos[idp - 1] * pos[idp];
        ll taken = neg[idn - 1] * neg[idn];
        if (takep >= taken) {
          ans = mul(mul(ans, inv(neg[idn - 1])), pos[idp]);
        } else {
          ans = mul(mul(ans, inv(pos[idp - 1])), neg[idn]);
        }
      } else if (idn == nn || idp == 0) {
        ans = mul(mul(ans, inv(neg[idn - 1])), pos[idp]);
      } else {
        ans = mul(mul(ans, inv(pos[idp - 1])), neg[idn]);
      }
    }
    cout << ans << '\\n';
  } else if (np) {
    int ans = 1;
    while (idp + idn < k && idp < np && idn < nn) {
      if (pos[idp] >= neg[idn]) {
        ans = mul(ans, pos[idp++]);
      } else
        ans = mul(ans, neg[idn++]);
    }
    while (idp + idn < k && idp < np) {
      ans = mul(ans, pos[idp++]);
    }
    while (idp + idn < k && idn < nn) {
      ans = mul(ans, pos[idn++]);
    }
    if (idn & 1) {
      if (idn < nn && 0 < idp && idp < np) {
        ll takep = pos[idp - 1] * pos[idp];
        ll taken = neg[idn - 1] * neg[idn];
        if (takep >= taken) {
          ans = mul(mul(ans, inv(neg[idn - 1])), pos[idp]);
        } else {
          ans = mul(mul(ans, inv(pos[idp - 1])), neg[idn]);
        }
      } else if (idn == nn || idp == 0) {
        ans = mul(mul(ans, inv(neg[idn - 1])), pos[idp]);
      } else {
        ans = mul(mul(ans, inv(pos[idp - 1])), neg[idn]);
      }
    }
    cout << ans << '\\n';
  } else {
    reverse(pos.begin(), pos.end());
    reverse(neg.begin(), neg.end());
    int ans = 1;
    while (idp + idn < k && idp < np && idn < nn) {
      if (pos[idp] < neg[idn]) {
        ans = mul(ans, pos[idp++]);
      } else
        ans = mul(ans, neg[idn++]);
    }
    while (idp + idn < k && idp < np) {
      ans = mul(ans, pos[idp++]);
    }
    while (idp + idn < k && idn < nn) {
      ans = mul(ans, neg[idn++]);
    }
    ans = (ans - MOD) % MOD;
    cout << abs(ans) << '\\n';
  }
}