Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

struct Combination {
  Int MOD, MAX_N;
  vector<Int> f, f_inv;

  //---------- 初期化 ----------
  Combination(Int mod = 1e9 + 7, Int max_n = 1e7) {
    MOD = mod;
    MAX_N = max_n;

    f = factorial(MAX_N);
    rep(i, f.size()) f_inv.push_back(inv(f[i]));
  }

  //------- べき乗 a^n -------
  Int power(Int a, Int n) {
    if (a == 0)
      return 0;
    if (n == 0)
      return 1;

    Int res = 1;
    while (n > 0) {
      if (n % 2 == 1)
        res = res * a % MOD;
      a = a * a % MOD;
      n /= 2;
    }
    return res;
  }

  //------- 逆元 1/a -------
  Int inv(Int a) { return power(a, MOD - 2); }

  //------- 階乗 (nまでの階乗の配列を返す) -------
  vector<Int> factorial(Int n) {
    vector<Int> res(n + 1, 1);
    rep1(i, n) res[i] = i * res[i - 1] % MOD;

    return res;
  }

  //------- 組み合わせ nCr -------
  Int comb(Int n, Int r) {
    if (r > n)
      return 0;
    return (f[n] * f_inv[r] % MOD) * f_inv[n - r] % MOD;
  }
};

int main() {
  Int N, twice;
  cin >> N;

  vector<Int> A(N + 1), cnt(N, 0);
  rep(i, N) {
    cin >> A[i];
    cnt[A[i] - 1]++;
    if (cnt[A[i] - 1] == 2)
      twice = A[i];
  }

  vector<int> pos;
  rep(i, N + 1) {
    if (A[i] == twice)
      pos.push_back(i);
  }

  Combination C(1e9 + 7, 1e5 + 10);
  rep1(k, N + 1) {
    Int ans = C.comb(N + 1, k);
    ans += C.MOD - C.comb(pos[0] + N - pos[1], k - 1);
    ans %= C.MOD;
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
typedef int64_t Int;

struct Combination {
  Int MOD, MAX_N;
  vector<Int> f, f_inv;

  //---------- 初期化 ----------
  Combination(Int mod = 1e9 + 7, Int max_n = 1e7) {
    MOD = mod;
    MAX_N = max_n;

    f = factorial(MAX_N);
    rep(i, f.size()) f_inv.push_back(inv(f[i]));
  }

  //------- べき乗 a^n -------
  Int power(Int a, Int n) {
    if (a == 0)
      return 0;
    if (n == 0)
      return 1;

    Int res = 1;
    while (n > 0) {
      if (n % 2 == 1)
        res = res * a % MOD;
      a = a * a % MOD;
      n /= 2;
    }
    return res;
  }

  //------- 逆元 1/a -------
  Int inv(Int a) { return power(a, MOD - 2); }

  //------- 階乗 (nまでの階乗の配列を返す) -------
  vector<Int> factorial(Int n) {
    vector<Int> res(n + 1, 1);
    rep1(i, n) res[i] = i * res[i - 1] % MOD;

    return res;
  }

  //------- 組み合わせ nCr -------
  Int comb(Int n, Int r) {
    if (r > n)
      return 0;
    return (f[n] * f_inv[r] % MOD) * f_inv[n - r] % MOD;
  }
};

int main() {
  Int N, twice;
  cin >> N;

  vector<Int> A(N + 1), cnt(N, 0);
  rep(i, N + 1) {
    cin >> A[i];
    cnt[A[i] - 1]++;
    if (cnt[A[i] - 1] == 2)
      twice = A[i];
  }

  vector<int> pos;
  rep(i, N + 1) {
    if (A[i] == twice)
      pos.push_back(i);
  }

  Combination C(1e9 + 7, 1e5 + 10);
  rep1(k, N + 1) {
    Int ans = C.comb(N + 1, k);
    ans += C.MOD - C.comb(pos[0] + N - pos[1], k - 1);
    ans %= C.MOD;
    cout << ans << endl;
  }
}