Problem p03674 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, (n)-1, 0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

#define MAX_N 2000005
ll inv[MAX_N], fac[MAX_N], finv[MAX_N];
void make() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX_N; i++) {
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    fac[i] = fac[i - 1] * (ll)i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
ll perm(ll n, ll r) {
  if (n < 0)
    return 0;
  else if (n < r)
    return 0;
  else
    return (fac[n] * finv[n - r]) % mod;
}
ll comb(ll n, ll r) {
  ll ans = 1;
  ans = perm(n, r);
  ans = (ans * finv[r]) % mod;
  return ans;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  make();
  int n;
  cin >> n;
  vector<int> A(n + 1);
  rep(i, n + 1) cin >> A[i];
  map<int, vector<int>> pos;
  rep(i, n) pos[A[i]].push_back(i);
  int p1, p2;
  for (auto ele : pos) {
    if (ele.second.size() == 2) {
      p1 = ele.second[0];
      p2 = ele.second[1];
    }
  }
  for (int i = 1; i <= n + 1; i++) {
    if (i == 1)
      cout << n << endk;
    else {
      ll ans = 0;
      (ans += comb(n - 1, i)) %= mod;
      (ans += comb(n - 1, i - 2)) %= mod;
      (ans += comb(n - 1, i - 1) * 2 - comb(p1 + n - p2, i - 1) + mod) %= mod;
      cout << ans << endk;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, (n)-1, 0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

#define MAX_N 2000005
ll inv[MAX_N], fac[MAX_N], finv[MAX_N];
void make() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX_N; i++) {
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    fac[i] = fac[i - 1] * (ll)i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
ll perm(ll n, ll r) {
  if (n < 0)
    return 0;
  else if (n < r)
    return 0;
  else
    return (fac[n] * finv[n - r]) % mod;
}
ll comb(ll n, ll r) {
  ll ans = 1;
  ans = perm(n, r);
  ans = (ans * finv[r]) % mod;
  return ans;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  make();
  int n;
  cin >> n;
  vector<int> A(n + 1);
  rep(i, n + 1) cin >> A[i];
  map<int, vector<int>> pos;
  rep(i, n + 1) pos[A[i]].push_back(i);
  int p1, p2;
  for (auto ele : pos) {
    if (ele.second.size() == 2) {
      p1 = ele.second[0];
      p2 = ele.second[1];
    }
  }
  for (int i = 1; i <= n + 1; i++) {
    if (i == 1)
      cout << n << endk;
    else {
      ll ans = 0;
      (ans += comb(n - 1, i)) %= mod;
      (ans += comb(n - 1, i - 2)) %= mod;
      (ans += comb(n - 1, i - 1) * 2 - comb(p1 + n - p2, i - 1) + mod) %= mod;
      cout << ans << endk;
    }
  }
  return 0;
}