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

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
const int mod = 1000000007;
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  if (n == k) {
    int ret = 1;
    rep(i, n) ret = (ret * a[i]) % mod;
    cout << (ret % mod + mod) % mod << endl;
    return 0;
  }
  sort(a.rbegin(), a.rend());
  if (*a.begin() < 0 && k % 2) {
    int ret = 1;
    rep(i, k) ret = (ret * a[i]) % mod;
    cout << (ret % mod + mod) % mod << endl;
    return 0;
  }
  vector<int> pos, neg;
  int zero = 0;
  rep(i, n) {
    if (a[i] > 0) {
      pos.push_back(a[i]);
    } else if (a[i] < 0) {
      neg.push_back(a[i]);
    } else {
      zero++;
    }
  }
  vector<int> pair;
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  if (pos.size() + neg.size() / 2 * 2 < k) {
    cout << 0 << endl;
    return 0;
  }
  int ret = 1;
  if (k % 2) {
    if (pos.size()) {
      ret = pos.back();
      pos.pop_back();
    } else {
      ret = 0;
    }
    k--;
  }
  reverse(pos.begin(), pos.end());
  for (int i = 0; i + 1 < pos.size(); i += 2) {
    pair.push_back(pos[i] * pos[i + 1]);
  }
  for (int i = 0; i + 1 < neg.size(); i += 2) {
    pair.push_back(neg[i] * neg[i + 1]);
  }
  if (pair.size())
    sort(pair.begin(), pair.end());
  k /= 2;
  while (k) {
    pair.back() %= mod;
    ret = (ret * pair.back()) % mod;
    k--;
    pair.pop_back();
  }
  cout << (ret % mod + mod) % mod << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
const int mod = 1000000007;
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  if (n == k) {
    int ret = 1;
    rep(i, n) ret = (ret * a[i]) % mod;
    cout << (ret % mod + mod) % mod << endl;
    return 0;
  }
  sort(a.rbegin(), a.rend());
  if (*a.begin() < 0 && k % 2) {
    int ret = 1;
    rep(i, k) ret = (ret * a[i]) % mod;
    cout << (ret % mod + mod) % mod << endl;
    return 0;
  }
  vector<int> pos, neg;
  int zero = 0;
  rep(i, n) {
    if (a[i] > 0) {
      pos.push_back(a[i]);
    } else if (a[i] < 0) {
      neg.push_back(a[i]);
    } else {
      zero++;
    }
  }
  vector<int> pair;
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  if (pos.size() + neg.size() / 2 * 2 < k) {
    cout << 0 << endl;
    return 0;
  }
  int ret = 1;
  if (k % 2) {
    ret = pos.back();
    pos.pop_back();
    k--;
  }
  reverse(pos.begin(), pos.end());
  for (int i = 0; i + 1 < pos.size(); i += 2) {
    pair.push_back(pos[i] * pos[i + 1]);
  }
  for (int i = 0; i + 1 < neg.size(); i += 2) {
    pair.push_back(neg[i] * neg[i + 1]);
  }
  if (pair.size())
    sort(pair.begin(), pair.end());
  k /= 2;
  while (k) {
    pair.back() %= mod;
    ret = (ret * pair.back()) % mod;
    k--;
  }
  cout << (ret % mod + mod) % mod << endl;
  return 0;
}