Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using Map = map<ll, ll>;
using T = tuple<ll, ll, ll>;
using vvl = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define print(v) cout << v << "\n";
#define fi(v) get<0>(v)
#define se(v) get<1>(v)
#define th(v) get<2>(v)
#define endl "\n"
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

#define MAX 10007
long long fac[10007], finv[10007], inv[10007];

// テーブルを作る前処理
void fact() {
  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 comb(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;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  fact();
  ll N;
  cin >> N;
  vector<ll> a(N + 1, 0);
  for (ll i = 0; i < N + 1; i++)
    cin >> a[i];
  ll A = INF, B = INF;
  Map M;
  for (ll i = 0; i < N + 1; i++)
    M[a[i]]++;
  ll mark;
  for (auto p : M)
    if (p.second == 2) {
      mark = p.first;
    }
  for (ll i = 0; i < N + 1; i++) {
    if (a[i] == mark && A == INF)
      A = i;
    else if (a[i] == mark)
      B = i;
  }
  print(N) for (ll i = 2; i <= N + 1; i++) {
    ll ans = 0;
    ans += comb(N - 1, i);
    ans += comb(N - 1, i - 2);
    ans -= comb(N + A - B, i - 1);
    ans += comb(N - 1, i - 1) * 2;
    print((ans + MOD) % MOD)
  }

  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using Map = map<ll, ll>;
using T = tuple<ll, ll, ll>;
using vvl = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define print(v) cout << v << "\n";
#define fi(v) get<0>(v)
#define se(v) get<1>(v)
#define th(v) get<2>(v)
#define endl "\n"
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

#define MAX 100007
long long fac[100007], finv[100007], inv[100007];

// テーブルを作る前処理
void fact() {
  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 comb(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;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  fact();
  ll N;
  cin >> N;
  vector<ll> a(N + 1, 0);
  for (ll i = 0; i < N + 1; i++)
    cin >> a[i];
  ll A = INF, B = INF;
  Map M;
  for (ll i = 0; i < N + 1; i++)
    M[a[i]]++;
  ll mark;
  for (auto p : M)
    if (p.second == 2) {
      mark = p.first;
    }
  for (ll i = 0; i < N + 1; i++) {
    if (a[i] == mark && A == INF)
      A = i;
    else if (a[i] == mark)
      B = i;
  }
  print(N) for (ll i = 2; i <= N + 1; i++) {
    ll ans = 0;
    ans += comb(N - 1, i);
    ans += comb(N - 1, i - 2);
    ans -= comb(N + A - B, i - 1);
    ans += comb(N - 1, i - 1) * 2;
    print((ans + MOD) % MOD)
  }

  return 0;
}