Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define siz(v) (ll)(v).size()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, x, n) for (ll i = x; i < (ll)(n); i++)
#define lb lower_bound
#define ub upper_bound
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const ll mod = 1000000007;
const ll INF = 1000000000000000099;
vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
// cin.tie(0);ios::sync_with_stdio(false);

const ll MAX = 100010; // 設定して

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod; // ＊１
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

signed main() {
  COMinit();
  ll n, ans = 0, lind = -1, rind, dup;
  cin >> n;
  vector<ll> v(n), cnt(n, 0);
  rep(i, n + 1) {
    cin >> v[i];
    cnt[v[i] - 1]++;
  }

  rep(i, n + 1) {
    if (cnt[i] == 2) {
      dup = i + 1;
      break;
    }
  }

  rep(i, n + 1) {
    if (v[i] == dup) {
      if (lind < 0)
        lind = i;
      else
        rind = i;
    }
  }

  for (int i = 1; i <= n + 1; i++) {
    ans = 0;
    ans += COM(n + 1, i);
    ans %= mod; // 2
    if (n - rind + lind >= i - 1) {
      ans += mod - COM(n - rind + lind, i - 1);
      ans %= mod;
    }
    // 1,0 1
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define siz(v) (ll)(v).size()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, x, n) for (ll i = x; i < (ll)(n); i++)
#define lb lower_bound
#define ub upper_bound
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const ll mod = 1000000007;
const ll INF = 1000000000000000099;
vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
// cin.tie(0);ios::sync_with_stdio(false);

const ll MAX = 100010; // 設定して

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod; // ＊１
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

signed main() {
  COMinit();
  ll n, ans = 0, lind = -1, rind, dup;
  cin >> n;
  vector<ll> v(n + 1), cnt(n, 0);
  rep(i, n + 1) {
    cin >> v[i];
    cnt[v[i] - 1]++;
  }

  rep(i, n + 1) {
    if (cnt[i] == 2) {
      dup = i + 1;
      break;
    }
  }

  rep(i, n + 1) {
    if (v[i] == dup) {
      if (lind < 0)
        lind = i;
      else
        rind = i;
    }
  }

  for (int i = 1; i <= n + 1; i++) {
    ans = 0;
    ans += COM(n + 1, i);
    ans %= mod; // 2
    if (n - rind + lind >= i - 1) {
      ans += mod - COM(n - rind + lind, i - 1);
      ans %= mod;
    }
    // 1,0 1
    cout << ans << endl;
  }
}