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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
int m_mul(int x, int y) { return 1LL * x * y % MOD; }
int m_mul(int x, int y, int tails, ...) { return m_mul(m_mul(x, y), tails); }
int m_pow(int x, int y) {
  int r = 1;
  while (y) {
    if (y & 1)
      r = m_mul(r, x);
    x = m_mul(x, x);
    y >>= 1;
  }
  return r;
}
int m_div(int x, int y) { return m_mul(x, m_pow(y, MOD - 2)); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> pos, neg, zero;
  for (int i = 0; i < n; i++) {
    int it;
    cin >> it;
    if (it > 0)
      pos.push_back(it);
    else if (it < 0)
      neg.push_back(it);
    else
      zero.push_back(it);
  }
  int ans = 1;
  if (n == k) {
    for (int it : pos)
      ans = m_mul(ans, it);
    for (int it : neg)
      ans = m_mul(ans, it);
    for (int it : zero)
      ans = m_mul(ans, it);
    if (ans < 0)
      ans += MOD;
  } else if (pos.size()) {
    int np = pos.size(), nn = neg.size();
    if (np + nn < k)
      ans = 0;
    else if (np + nn == k && nn % 2 == 1)
      ans = 0;
    else {
      sort(pos.begin(), pos.end(), greater<int>());
      sort(neg.begin(), neg.end());
      int ni = 0, pi = 0;
      for (int i = 0; i < k; i++) {
        if (ni == nn || (pi < np && pos[pi] >= -neg[ni]))
          pi++;
        else
          ni++;
      }
      if (ni % 2 == 1) {
        ll nxp = pi == np ? 0 : pos[pi];
        ll lsp = pi == 0 ? 1 : pos[pi - 1];
        ll nxn = ni == nn ? 0 : -neg[ni];
        ll lsn = -neg[ni - 1];
        if (pi == 0 || lsp * nxp > lsn * nxn)
          pi++, ni--;
        else
          pi--, ni++;
      }
      for (int i = 0; i < pi; i++)
        ans = m_mul(ans, pos[i]);
      for (int i = 0; i < ni; i++)
        ans = m_mul(ans, -neg[i]);
    }
  } else {
    if (k % 2 == 1) {
      if (zero.size())
        ans = 0;
      else {
        sort(neg.begin(), neg.end(), greater<int>());
        for (int i = 0; i < k; i++)
          ans = m_mul(ans, -neg[i]);
        ans = MOD - ans;
      }
    } else {
      if ((int)neg.size() < k)
        ans = 0;
      else {
        sort(neg.begin(), neg.end());
        for (int i = 0; i < k; i++)
          ans = m_mul(ans, neg[i]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
int m_mul(int x, int y) { return 1LL * x * y % MOD; }
int m_mul(int x, int y, int tails, ...) { return m_mul(m_mul(x, y), tails); }
int m_pow(int x, int y) {
  int r = 1;
  while (y) {
    if (y & 1)
      r = m_mul(r, x);
    x = m_mul(x, x);
    y >>= 1;
  }
  return r;
}
int m_div(int x, int y) { return m_mul(x, m_pow(y, MOD - 2)); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> pos, neg, zero;
  for (int i = 0; i < n; i++) {
    int it;
    cin >> it;
    if (it > 0)
      pos.push_back(it);
    else if (it < 0)
      neg.push_back(it);
    else
      zero.push_back(it);
  }
  int ans = 1;
  if (n == k) {
    for (int it : pos)
      ans = m_mul(ans, it);
    for (int it : neg)
      ans = m_mul(ans, it);
    for (int it : zero)
      ans = m_mul(ans, it);
    if (ans < 0)
      ans += MOD;
  } else if (pos.size()) {
    int np = pos.size(), nn = neg.size();
    if (np + nn < k)
      ans = 0;
    else {
      sort(pos.begin(), pos.end(), greater<int>());
      sort(neg.begin(), neg.end());
      int ni = 0, pi = 0;
      for (int i = 0; i < k; i++) {
        if (pos[pi] >= -neg[ni])
          pi++;
        else
          ni++;
      }
      if (ni % 2 == 1) {
        ll nxp = pi == np ? 0 : pos[pi];
        ll lsp = pi == 0 ? 1 : pos[pi - 1];
        ll nxn = ni == np ? 0 : -neg[ni];
        ll lsn = -neg[ni - 1];
        if (lsp * nxp >= lsn * nxn)
          pi++, ni--;
        else
          pi--, ni++;
      }
      for (int i = 0; i < pi; i++)
        ans = m_mul(ans, pos[i]);
      for (int i = 0; i < ni; i++)
        ans = m_mul(ans, -neg[i]);
    }
  } else {
    if (k % 2 == 1) {
      if (zero.size())
        ans = 0;
      else {
        sort(neg.begin(), neg.end(), greater<int>());
        for (int i = 0; i < k; i++)
          ans = m_mul(ans, neg[i]);
      }
    } else {
      if ((int)neg.size() < k)
        ans = 0;
      else {
        sort(neg.begin(), neg.end());
        for (int i = 0; i < k; i++)
          ans = m_mul(ans, neg[i]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}