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

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif

template <class T, class Op = multiplies<T>>
constexpr T power(T a, long long n, Op op = Op(), T e = {1}) {
  assert(n >= 0);
  while (n) {
    if (n & 1)
      e = op(e, a);
    if (n >>= 1)
      a = op(a, a);
  }
  return e;
}

template <unsigned M> struct modular {
  using m = modular;
  static constexpr unsigned mod = M;
  unsigned v;
  modular(long long x = 0) : v((x %= mod) < 0 ? x + mod : x) {}
  m operator-() const { return m() -= *this; }
  m &operator+=(m b) {
    if ((int)(v += b.v - mod) < 0)
      v += mod;
    return *this;
  }
  m &operator-=(m b) {
    if ((int)(v -= b.v) < 0)
      v += mod;
    return *this;
  }
  m &operator*=(m b) {
    v = (uint64_t)v * b.v % mod;
    return *this;
  }
  m &operator/=(m b) { return *this *= power(b, mod - 2); }
  friend m operator+(m a, m b) { return a += b; }
  friend m operator-(m a, m b) { return a -= b; }
  friend m operator*(m a, m b) { return a *= b; }
  friend m operator/(m a, m b) { return a /= b; }
  friend bool operator==(m a, m b) { return a.v == b.v; }
};

using mint = modular<power(10, 9) + 7>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<long long> a(n);
  int pos = 0, zero = 0, neg = 0;
  for (auto &&e : a) {
    cin >> e;
    pos += e > 0;
    zero += e == 0;
    neg += e < 0;
  }
  bool ok = k <= n - zero;
  ok &= k < n - zero or neg % 2 == 0;
  ok &= pos > 0 or k % 2 == 0;
  DEBUG(ok);
  if (ok) {
    sort(begin(a), end(a), [](int x, int y) { return abs(x) > abs(y); });
    while (a.back() == 0) {
      a.pop_back();
    }
    DEBUG(a, k);
    int cnt = count_if(begin(a), begin(a) + k, [](int x) { return x < 0; });
    if (cnt & 1) {
      int p = k - 1;
      while (a[p] > 0) {
        --p;
      }
      int q =
          find_if(begin(a) + k, end(a), [](int x) { return x > 0; }) - begin(a);
      DEBUG(p, q);
      int r = k - 1;
      while (r >= 0 and a[r] < 0) {
        --r;
      }
      int s =
          find_if(begin(a) + k, end(a), [](int x) { return x < 0; }) - begin(a);
      DEBUG(r, s);
      if (q < n - zero and (r == -1 or a[r] < 0 or s == n - zero or
                            abs(a[q] * a[r]) > abs(a[p] * a[s]))) {
        swap(a[p], a[q]);
      } else {
        swap(a[r], a[s]);
      }
    }
    mint res = accumulate(begin(a), begin(a) + k, mint(1), multiplies<>());
    cout << res.v << '\\n';
  } else if (zero) {
    cout << "0\\n";
  } else {
    sort(begin(a), end(a), [](int x, int y) { return abs(x) < abs(y); });
    mint res = accumulate(begin(a), begin(a) + k, mint(1), multiplies<>());
    cout << res.v << '\\n';
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif

template <class T, class Op = multiplies<T>>
constexpr T power(T a, long long n, Op op = Op(), T e = {1}) {
  assert(n >= 0);
  while (n) {
    if (n & 1)
      e = op(e, a);
    if (n >>= 1)
      a = op(a, a);
  }
  return e;
}

template <unsigned M> struct modular {
  using m = modular;
  static constexpr unsigned mod = M;
  unsigned v;
  modular(long long x = 0) : v((x %= mod) < 0 ? x + mod : x) {}
  m operator-() const { return m() -= *this; }
  m &operator+=(m b) {
    if ((int)(v += b.v - mod) < 0)
      v += mod;
    return *this;
  }
  m &operator-=(m b) {
    if ((int)(v -= b.v) < 0)
      v += mod;
    return *this;
  }
  m &operator*=(m b) {
    v = (uint64_t)v * b.v % mod;
    return *this;
  }
  m &operator/=(m b) { return *this *= power(b, mod - 2); }
  friend m operator+(m a, m b) { return a += b; }
  friend m operator-(m a, m b) { return a -= b; }
  friend m operator*(m a, m b) { return a *= b; }
  friend m operator/(m a, m b) { return a /= b; }
  friend bool operator==(m a, m b) { return a.v == b.v; }
};

using mint = modular<power(10, 9) + 7>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<long long> a(n);
  int pos = 0, zero = 0, neg = 0;
  for (auto &&e : a) {
    cin >> e;
    pos += e > 0;
    zero += e == 0;
    neg += e < 0;
  }
  bool ok = k <= n - zero;
  if (k & 1) {
    ok = pos > 0;
  } else {
    ok = k < n or neg % 2 == 0;
  }
  if (ok) {
    sort(begin(a), end(a), [](int x, int y) { return abs(x) > abs(y); });
    while (a.back() == 0) {
      a.pop_back();
    }
    int cnt = count_if(begin(a), begin(a) + k, [](int x) { return x < 0; });
    if (cnt & 1) {
      int p = k - 1;
      while (a[p] < 0) {
        --p;
      }
      int q =
          find_if(begin(a) + k, end(a), [](int x) { return x < 0; }) - begin(a);
      int r = k - 1;
      while (r >= 0 and a[r] > 0) {
        --r;
      }
      int s =
          find_if(begin(a) + k, end(a), [](int x) { return x > 0; }) - begin(a);
      if (r == -1 or a[r] > 0 or abs(a[q] * a[r]) > abs(a[p] * a[s])) {
        swap(a[p], a[q]);
      } else {
        swap(a[r], a[s]);
      }
    }
    mint res = accumulate(begin(a), begin(a) + k, mint(1), multiplies<>());
    cout << res.v << '\\n';
  } else if (zero) {
    cout << "0\\n";
  } else {
    sort(begin(a), end(a), [](int x, int y) { return abs(x) < abs(y); });
    mint res = accumulate(begin(a), begin(a) + k, mint(1), multiplies<>());
    cout << res.v << '\\n';
  }
}