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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define all(v) v.begin(), v.end()
using pint = pair<int, int>;
using tint = tuple<int, int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef int64_t lint;
constexpr double PI = 3.14159265358979323846;
constexpr int INF = INT_MAX / 2;
constexpr lint LINF = LLONG_MAX / 2;

lint mod = 1000000007;
struct mint {
  lint x; // typedef long long ll;
  mint(lint x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(lint t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int N, K;
  cin >> N >> K;
  vector<lint> v(N);
  rep(i, N) { cin >> v.at(i); }

  lint b = 0, c = N - 1, x, y, z;
  mint a = 1;
  sort(all(v));
  if ((int(v.at(N - 1)) <= 0 && K % 2 == 1) || N == K) {
    rep(i, K) { a *= v.at(N - 1 - i); }
    std::cout << a << '\\n';
    return 0;
  }
  rep(i, K) {
    if (i != K - 1 && v.at(b) * v.at(b + 1) > v.at(c) * v.at(c - 1)) {
      a *= v.at(b);
      a *= v.at(b + 1);
      b += 2;
      i++;
    } else {
      a *= v.at(c);
      c--;
    }
  }
  cout << a << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define all(v) v.begin(), v.end()
using pint = pair<int, int>;
using tint = tuple<int, int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef int64_t lint;
constexpr double PI = 3.14159265358979323846;
constexpr int INF = INT_MAX / 2;
constexpr lint LINF = LLONG_MAX / 2;

lint mod = 1000000007;

int main() {
  int N, K;
  cin >> N >> K;
  vector<lint> v(N);
  rep(i, N) { cin >> v.at(i); }

  lint a = 1, b = 0, c = N - 1, x, y, z;
  vector<lint> t;
  sort(all(v));
  if ((v.at(N - 1) <= 0 && K % 2 == 1) || N == K) {
    rep(i, K) {
      a *= v.at(N - 1 - i);
      if (a >= mod || a <= -mod)
        a %= mod;
    }
    if (a < 0)
      a += mod;
    std::cout << a << '\\n';
    return 0;
  }
  rep(i, K) {
    if (abs(v.at(b)) > abs(v.at(c))) {
      y = a;
      a *= v.at(b);
      b++;
      t.clear();
    } else {
      a *= v.at(c);
      t.push_back(v.at(c));
      c--;
    }
    x = a;
    if (a >= mod || a <= -mod)
      a %= mod;
  }
  if (a < 0) {
    a = x;
    if (c == N - 1) {
      a /= v.at(b - 1);
      a *= v.at(c);
    } else {
      if (c * (c + 1) > b * (b - 1)) {
        a = y;
        rep(i, t.size()) {
          a *= t.at(i);
          if (a >= mod)
            a %= mod;
        }
        a *= v.at(c);
      } else {
        a = y;
        rep(i, t.size() - 1) {
          a *= t.at(i);
          if (a >= mod)
            a %= mod;
        }
        a *= v.at(b - 1);
        if (a >= mod || a <= -mod)
          a %= mod;
        a *= v.at(b);
      }
    }
    if (a >= mod)
      a %= mod;
  }

  std::cout << a << '\\n';
}