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

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9 + 7;
constexpr llong inf = mod * mod;

llong moduro(llong n) {
  if (n > 0) {
    return n % mod;
  } else {
    return n + (-n / mod + 1) * mod;
  }
}

int main() {
  llong N, K, zero = 0;
  vector<llong> A, plus, minus;

  cin >> N >> K;
  A.resize(N);
  for (auto &in : A) {
    cin >> in;
    if (in > 0) {
      plus.push_back(in);
    } else if (in < 0) {
      minus.push_back(in);
    } else {
      zero++;
    }
  }

  llong ans = 1;
  if (plus.size() == 0) {
    if (minus.size() < K) {
      ans = 0;
    } else if (K % 2 == 0) {
      sort(ALL(minus));
      for (int i = 0; i < K; ++i) {
        ans = moduro(ans * minus[i]);
      }
    } else if (zero > 0) {
      ans = 0;
    } else {
      sort(ALL(minus), greater<>());
      for (int i = 0; i < K; ++i) {
        ans = moduro(ans * minus[i]);
      }
    }
  } else if (minus.size() + plus.size() < K) {
    ans = 0;
  } else if (minus.size() + plus.size() == K) {
    if (minus.size() % 2 == 0) {
      for (auto m : minus) {
        ans = moduro(ans * m);
      }
      for (auto p : plus) {
        ans = moduro(ans * p);
      }
    } else {
      if (zero > 0) {
        ans = 0;
      } else {
        for (auto m : minus) {
          ans = moduro(ans * m);
        }
        for (auto p : plus) {
          ans = moduro(ans * p);
        }
      }
    }
  } else {
    sort(ALL(plus), greater<>());
    sort(ALL(minus));
    int pid = 0, mid = 0;
    for (int i = 0; i < K; ++i) {
      if (mid < int(minus.size()) - 1 && i < K - 1) {
        if (pid >= int(plus.size() - 1)) {
          ans = moduro(ans * moduro(minus[mid] * minus[mid + 1]));
          mid += 2;
          i += 1;
        } else {
          llong pp = plus[pid] * plus[pid + 1];
          llong mm = minus[mid] * minus[mid + 1];
          if (pp > mm) {
            ans = moduro(ans * moduro(pp));
            pid += 2;
          } else {
            ans = moduro(ans * moduro(mm));
            mid += 2;
          }
          i += 1;
        }
      } else {
        ans = moduro(ans * plus[pid++]);
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9 + 7;
constexpr llong inf = mod * mod;

llong moduro(llong n) {
  if (n > 0) {
    return n % mod;
  } else {
    return n + (-n / mod + 1) * mod;
  }
}

int main() {
  llong N, K, zero = 0;
  vector<llong> A, plus, minus;

  cin >> N >> K;
  A.resize(N);
  for (auto &in : A) {
    cin >> in;
    if (in > 0) {
      plus.push_back(in);
    } else if (in < 0) {
      minus.push_back(in);
    } else {
      zero++;
    }
  }

  llong ans = 1;
  if (plus.size() == 0) {
    if (minus.size() < K) {
      ans = 0;
    } else if (K % 2 == 0) {
      sort(ALL(minus));
      for (int i = 0; i < K; ++i) {
        ans = moduro(ans * minus[i]);
      }
    } else if (zero > 0) {
      ans = 0;
    } else {
      sort(ALL(minus), greater<>());
      for (int i = 0; i < K; ++i) {
        ans = moduro(ans * minus[i]);
      }
    }
  } else if (minus.size() + plus.size() < K) {
    ans = 0;
  } else if (minus.size() + plus.size() == K) {
    if (minus.size() % 2 == 0) {
      for (auto m : minus) {
        ans = moduro(ans * m);
      }
      for (auto p : plus) {
        ans = moduro(ans * p);
      }
    } else {
      if (zero > 0) {
        ans = 0;
      } else {
        for (auto m : minus) {
          ans = moduro(ans * m);
        }
        for (auto p : minus) {
          ans = moduro(ans * p);
        }
      }
    }
    return 1;
  } else {
    sort(ALL(plus), greater<>());
    sort(ALL(minus));
    int pid = 0, mid = 0;
    for (int i = 0; i < K; ++i) {
      if (mid < int(minus.size()) - 1 && i < K - 1) {
        if (pid >= int(plus.size() - 1)) {
          ans = moduro(ans * moduro(minus[mid] * minus[mid + 1]));
          mid += 2;
          i += 1;
        } else {
          llong pp = plus[pid] * plus[pid + 1];
          llong mm = minus[mid] * minus[mid + 1];
          if (pp > mm) {
            ans = moduro(ans * moduro(pp));
            pid += 2;
          } else {
            ans = moduro(ans * moduro(mm));
            mid += 2;
          }
          i += 1;
        }
      } else {
        ans = moduro(ans * plus[pid++]);
      }
    }
  }

  cout << ans << endl;

  return 0;
}