Problem p03674 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

template <typename T, long long MOD_VALUE> class ModInt {
  static constexpr long long MOD = MOD_VALUE;

private:
  T value_;

public:
  ModInt() {}
  ModInt(const T &value) {
    if (value >= 0) {
      value_ = value % MOD;
    } else {
      T k = (MOD - 1 - value) / MOD;
      value_ = (value + k * MOD) % MOD;
    }
  }
  ModInt &operator+=(const ModInt &x) {
    value_ += x.value_;
    if (value_ >= MOD)
      value_ -= MOD;
    return *this;
  }
  friend ModInt &operator+=(const T &x, const ModInt &y) {
    ModInt res(x);
    res.value_ += x.value_;
    if (res.value_ >= MOD)
      res.value_ -= MOD;
    return res;
  }
  ModInt &operator-=(const ModInt &x) {
    if (value_ < x.value_)
      value_ += MOD;
    value_ -= x.value_;
    return *this;
  }
  friend ModInt &operator-=(const T &x, const ModInt &y) {
    ModInt res(x);
    if (res.value_ < y.value_)
      res.value_ += MOD;
    res.value_ -= y.value_;
    return res;
  }
  ModInt &operator*=(const ModInt &x) {
    value_ = (value_ * x.value_) % MOD;
    return *this;
  }
  friend ModInt &operator*=(const T &x, const ModInt &y) {
    ModInt res(x);
    res.value_ = (res.value_ * y.value_) % MOD;
    return res;
  }
  const ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  friend const ModInt operator+(const T &x, const ModInt &y) {
    return ModInt(x) += y;
  }
  const ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  friend const ModInt operator-(const T &x, const ModInt &y) {
    return ModInt(x) -= y;
  }
  const ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
  friend const ModInt operator*(const T &x, const ModInt &y) {
    return ModInt(x) *= y;
  }
  static ModInt modpow(ModInt x, long long y) {
    ModInt z = 1;
    while (y > 0) {
      if (y & 1) {
        z *= x;
      }
      x *= x;
      y /= 2;
    }
    return z;
  }
  ModInt &operator/=(const ModInt &x) { return *this *= modpow(x, MOD - 2); }
  const ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; }
  friend const ModInt operator/(const T &x, const ModInt &y) {
    return ModInt(x) /= y;
  }
  ModInt operator++(int) {
    ModInt tmp(*this);
    value_ = (value_ + 1 == MOD ? 0 : value_ + 1);
    return tmp;
  }
  ModInt operator--(int) {
    ModInt tmp(*this);
    value_ = (value_ - 1 < 0 ? MOD - 1 : value_ - 1);
    return tmp;
  }
  friend istream &operator>>(istream &stream, ModInt &x) {
    stream >> x.value_;
    x.value_ %= MOD;
    return stream;
  }
  friend ostream &operator<<(ostream &stream, const ModInt &x) {
    stream << x.value_;
    return stream;
  }
};

template <typename T, long long MOD> class Combination {
  using mint = ModInt<T, MOD>;

private:
  vector<mint> factorial_, inv_factorial_, inv_;

public:
  Combination(int max_num)
      : factorial_(max_num + 1), inv_factorial_(max_num + 1),
        inv_(max_num + 1) {
    factorial_[0] = factorial_[1] = 1;
    inv_factorial_[0] = inv_factorial_[1] = 1;
    inv_[1] = 1;
    for (int i = 2; i <= max_num; i++) {
      factorial_[i] = factorial_[i - 1] * i;
      inv_[i] = MOD - inv_[MOD % i] * (MOD / i);
      inv_factorial_[i] = inv_factorial_[i - 1] * inv_[i];
    }
  }
  mint calc(int n, int k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return factorial_[n] * inv_factorial_[k] * inv_factorial_[n - k];
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N + 1);
  for (auto &a : A) {
    cin >> a;
  }
  int idx1, idx2;
  vector<vector<int>> cnt(N);
  rep(i, 0, N) { cnt[A[i] - 1].push_back(i); }
  rep(i, 0, N) {
    if (cnt[i].size() == 2) {
      idx1 = cnt[i].front();
      idx2 = cnt[i].back();
      break;
    }
  }
  Combination<ll, 1000000007> comb(2 * N + 1);
  rep(k, 1, N + 2) {
    cout << comb.calc(N + 1, k) - comb.calc(idx1 + N - idx2, k - 1) << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return 0;
}

template <typename T, long long MOD_VALUE> class ModInt {
  static constexpr long long MOD = MOD_VALUE;

private:
  T value_;

public:
  ModInt() {}
  ModInt(const T &value) {
    if (value >= 0) {
      value_ = value % MOD;
    } else {
      T k = (MOD - 1 - value) / MOD;
      value_ = (value + k * MOD) % MOD;
    }
  }
  ModInt &operator+=(const ModInt &x) {
    value_ += x.value_;
    if (value_ >= MOD)
      value_ -= MOD;
    return *this;
  }
  friend ModInt &operator+=(const T &x, const ModInt &y) {
    ModInt res(x);
    res.value_ += x.value_;
    if (res.value_ >= MOD)
      res.value_ -= MOD;
    return res;
  }
  ModInt &operator-=(const ModInt &x) {
    if (value_ < x.value_)
      value_ += MOD;
    value_ -= x.value_;
    return *this;
  }
  friend ModInt &operator-=(const T &x, const ModInt &y) {
    ModInt res(x);
    if (res.value_ < y.value_)
      res.value_ += MOD;
    res.value_ -= y.value_;
    return res;
  }
  ModInt &operator*=(const ModInt &x) {
    value_ = (value_ * x.value_) % MOD;
    return *this;
  }
  friend ModInt &operator*=(const T &x, const ModInt &y) {
    ModInt res(x);
    res.value_ = (res.value_ * y.value_) % MOD;
    return res;
  }
  const ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  friend const ModInt operator+(const T &x, const ModInt &y) {
    return ModInt(x) += y;
  }
  const ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  friend const ModInt operator-(const T &x, const ModInt &y) {
    return ModInt(x) -= y;
  }
  const ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
  friend const ModInt operator*(const T &x, const ModInt &y) {
    return ModInt(x) *= y;
  }
  static ModInt modpow(ModInt x, long long y) {
    ModInt z = 1;
    while (y > 0) {
      if (y & 1) {
        z *= x;
      }
      x *= x;
      y /= 2;
    }
    return z;
  }
  ModInt &operator/=(const ModInt &x) { return *this *= modpow(x, MOD - 2); }
  const ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; }
  friend const ModInt operator/(const T &x, const ModInt &y) {
    return ModInt(x) /= y;
  }
  ModInt operator++(int) {
    ModInt tmp(*this);
    value_ = (value_ + 1 == MOD ? 0 : value_ + 1);
    return tmp;
  }
  ModInt operator--(int) {
    ModInt tmp(*this);
    value_ = (value_ - 1 < 0 ? MOD - 1 : value_ - 1);
    return tmp;
  }
  friend istream &operator>>(istream &stream, ModInt &x) {
    stream >> x.value_;
    x.value_ %= MOD;
    return stream;
  }
  friend ostream &operator<<(ostream &stream, const ModInt &x) {
    stream << x.value_;
    return stream;
  }
};

template <typename T, long long MOD> class Combination {
  using mint = ModInt<T, MOD>;

private:
  vector<mint> factorial_, inv_factorial_, inv_;

public:
  Combination(int max_num)
      : factorial_(max_num + 1), inv_factorial_(max_num + 1),
        inv_(max_num + 1) {
    factorial_[0] = factorial_[1] = 1;
    inv_factorial_[0] = inv_factorial_[1] = 1;
    inv_[1] = 1;
    for (int i = 2; i <= max_num; i++) {
      factorial_[i] = factorial_[i - 1] * i;
      inv_[i] = MOD - inv_[MOD % i] * (MOD / i);
      inv_factorial_[i] = inv_factorial_[i - 1] * inv_[i];
    }
  }
  mint calc(int n, int k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return factorial_[n] * inv_factorial_[k] * inv_factorial_[n - k];
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N + 1);
  for (auto &a : A) {
    cin >> a;
  }
  int idx1, idx2;
  vector<vector<int>> cnt(N);
  rep(i, 0, N + 1) { cnt[A[i] - 1].push_back(i); }
  rep(i, 0, N) {
    if (cnt[i].size() == 2) {
      idx1 = cnt[i].front();
      idx2 = cnt[i].back();
      break;
    }
  }
  Combination<ll, 1000000007> comb(2 * N + 1);
  rep(k, 1, N + 2) {
    cout << comb.calc(N + 1, k) - comb.calc(idx1 + N - idx2, k - 1) << endl;
  }
  return 0;
}