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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <random>
constexpr int maxn = 200005;

using LL = long long;
template <int _mod> struct Int {
  static constexpr int MOD = _mod;
  int v = 0;
  Int() {}
  template <typename T> Int(T _v) { *this = _v; }
  Int(const Int &v) : v(v.v) {}
  template <typename T> Int &operator=(T _v) {
    v = (-MOD < _v && _v < MOD)
            ? _v
            : (MOD <= _v && _v < (MOD << 1) ? _v - MOD : _v % MOD);
    if (v < 0)
      v += MOD;
    return *this;
  }
  Int &operator=(const Int &v) {
    this->v = v.v;
    return *this;
  }
  template <typename T> inline static Int quick_pow(Int a, T n) {
    Int ans = 1;
    while (n) {
      if (n & 1)
        ans *= a;
      a *= a;
      n >>= 1;
    }
    return ans;
  }
  inline static Int inv(Int v) { return quick_pow(v, MOD - 2); }
  friend inline int mod(int v) { return v + (v >> 31 & MOD); }
  friend inline void mod(int &v) { v += v >> 31 & MOD; }
  inline bool operator<(const Int &rhs) const { return v < rhs.v; }
  inline bool operator>(const Int &rhs) const { return v > rhs.v; }
  inline bool operator<=(const Int &rhs) const { return v <= rhs.v; }
  inline bool operator>=(const Int &rhs) const { return v >= rhs.v; }
  inline bool operator==(const Int &rhs) const { return v == rhs.v; }
  inline bool operator!=(const Int &rhs) const { return v != rhs.v; }
  inline Int operator+(const Int &rhs) const { return Int(v + rhs.v - MOD); }
  inline Int operator-(const Int &rhs) const { return Int(v - rhs.v); }
  inline Int operator*(const Int &rhs) const {
    return Int((long long)v * rhs.v);
  }
  inline Int operator/(const Int &rhs) const { return operator*(inv(rhs)); }
  inline Int operator+=(const Int &rhs) { return *this = *this + rhs; }
  inline Int operator-=(const Int &rhs) { return *this = *this - rhs; }
  inline Int operator*=(const Int &rhs) { return *this = *this * rhs; }
  inline Int operator/=(const Int &rhs) { return *this = *this / rhs; }
  inline Int operator-() const { return Int(-v); }
  inline Int &operator++() { return *this += 1; }
  inline Int &operator--() { return *this -= 1; }
  inline Int operator++(int) {
    const Int r = *this;
    *this += 1;
    return r;
  }
  inline Int operator--(int) {
    const Int r = *this;
    *this -= 1;
    return r;
  }
};
#include <cassert>
#include <cstdlib>
using INT = Int<1000000007>;

int A[maxn], B[maxn];
bool vis[maxn];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &A[i]);
  std::sort(A + 1, A + n + 1);
  int nega = std::count_if(A + 1, A + n + 1, [](int x) { return x < 0; });
  int zero = std::count(A + 1, A + n + 1, 0);
  int post = std::count_if(A + 1, A + n + 1, [](int x) { return x > 0; });
  if (k > nega + post) {
    printf("%d\\n", 0);
    return 0;
  }
  int m = 0;
  for (int i = 1; i <= n; i++)
    if (A[i])
      A[++m] = A[i];
  if ((!post && (k & 1)) || (post == 1 && ((k - 1) & 1))) {
    if (zero)
      return puts("0"), 0;
    std::sort(A + 1, A + m + 1,
              [](int x, int y) { return std::abs(x) < std::abs(y); });
    INT ans = 1;
    for (int i = 1; i <= k; i++)
      ans *= std::abs(A[i]);
    printf("%d\\n", (-ans).v);
    return 0;
  }
  for (int i = 1, j = m; k;) {
    if (k >= 2) {
      /*if (A[i] < 0 && A[i + 1] > 0) {
          vis[j] = vis[j - 1] = true;
          j -= 2;
          k -= 2;
      }
      else if (A[j] > 0 && A[j - 1] < 0) {
          vis[i] = vis[i + 1] = true;
          i += 2;
          k -= 2;
      }
      else {*/
      LL x1 = 1LL * A[i] * A[i + 1];
      LL x2 = 1LL * A[j] * A[j - 1];
      if (x1 > x2)
        vis[i] = vis[i + 1] = true, i += 2, k -= 2;
      else
        vis[j] = true, j--, k--;
      // }
    } else {
      vis[j--] = true;
      k--;
    }
  }
  INT ans = 1;
  for (int i = 1; i <= n; i++)
    if (vis[i])
      ans *= A[i];
  printf("%d\\n", ans.v);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <random>
constexpr int maxn = 200005;

using LL = long long;
template <int _mod> struct Int {
  static constexpr int MOD = _mod;
  int v = 0;
  Int() {}
  template <typename T> Int(T _v) { *this = _v; }
  Int(const Int &v) : v(v.v) {}
  template <typename T> Int &operator=(T _v) {
    v = (-MOD < _v && _v < MOD)
            ? _v
            : (MOD <= _v && _v < (MOD << 1) ? _v - MOD : _v % MOD);
    if (v < 0)
      v += MOD;
    return *this;
  }
  Int &operator=(const Int &v) {
    this->v = v.v;
    return *this;
  }
  template <typename T> inline static Int quick_pow(Int a, T n) {
    Int ans = 1;
    while (n) {
      if (n & 1)
        ans *= a;
      a *= a;
      n >>= 1;
    }
    return ans;
  }
  inline static Int inv(Int v) { return quick_pow(v, MOD - 2); }
  friend inline int mod(int v) { return v + (v >> 31 & MOD); }
  friend inline void mod(int &v) { v += v >> 31 & MOD; }
  inline bool operator<(const Int &rhs) const { return v < rhs.v; }
  inline bool operator>(const Int &rhs) const { return v > rhs.v; }
  inline bool operator<=(const Int &rhs) const { return v <= rhs.v; }
  inline bool operator>=(const Int &rhs) const { return v >= rhs.v; }
  inline bool operator==(const Int &rhs) const { return v == rhs.v; }
  inline bool operator!=(const Int &rhs) const { return v != rhs.v; }
  inline Int operator+(const Int &rhs) const { return Int(v + rhs.v - MOD); }
  inline Int operator-(const Int &rhs) const { return Int(v - rhs.v); }
  inline Int operator*(const Int &rhs) const {
    return Int((long long)v * rhs.v);
  }
  inline Int operator/(const Int &rhs) const { return operator*(inv(rhs)); }
  inline Int operator+=(const Int &rhs) { return *this = *this + rhs; }
  inline Int operator-=(const Int &rhs) { return *this = *this - rhs; }
  inline Int operator*=(const Int &rhs) { return *this = *this * rhs; }
  inline Int operator/=(const Int &rhs) { return *this = *this / rhs; }
  inline Int operator-() const { return Int(-v); }
  inline Int &operator++() { return *this += 1; }
  inline Int &operator--() { return *this -= 1; }
  inline Int operator++(int) {
    const Int r = *this;
    *this += 1;
    return r;
  }
  inline Int operator--(int) {
    const Int r = *this;
    *this -= 1;
    return r;
  }
};
#include <cassert>
#include <cstdlib>
using INT = Int<1000000007>;

int A[maxn], B[maxn];
bool vis[maxn];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &A[i]);
  std::sort(A + 1, A + n + 1);
  int nega = std::count_if(A + 1, A + n + 1, [](int x) { return x < 0; });
  int zero = std::count(A + 1, A + n + 1, 0);
  int post = std::count_if(A + 1, A + n + 1, [](int x) { return x > 0; });
  if (k > nega + post) {
    printf("%d\\n", 0);
    return 0;
  }
  int m = 0;
  for (int i = 1; i <= n; i++)
    if (A[i])
      A[++m] = A[i];
  if ((!post && (k & 1)) || (post == 1 && ((k - 1) & 1))) {
    if (zero)
      return puts("0"), 0;
    std::sort(A + 1, A + m + 1,
              [](int x, int y) { return std::abs(x) < std::abs(y); });
    INT ans = 1;
    for (int i = 1; i <= k; i++)
      ans *= std::abs(A[i]);
    printf("%d\\n", (-ans).v);
    return 0;
  }
  for (int i = 1, j = m; k;) {
    if (k >= 2) {
      if (A[i] < 0 && A[i + 1] > 0) {
        vis[j] = vis[j - 1] = true;
        j -= 2;
        k -= 2;
      } else if (A[j] > 0 && A[j - 1] < 0) {
        vis[i] = vis[i + 1] = true;
        i += 2;
        k -= 2;
      } else {
        LL x1 = 1LL * A[i] * A[i + 1];
        LL x2 = 1LL * A[j] * A[j - 1];
        if (x1 > x2)
          vis[i] = vis[i + 1] = true, i += 2;
        else
          vis[j] = vis[j - 1] = true, j -= 2;
        k -= 2;
      }
    } else {
      if (A[j] < 0)
        abort();
      vis[j--] = true;
      k--;
    }
  }
  INT ans = 1;
  for (int i = 1; i <= n; i++)
    if (vis[i])
      ans *= A[i];
  printf("%d\\n", ans.v);
  return 0;
}