Problem p03676 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define ALL(c) begin(c), end(c)
#define RALL(c) rbegin(c), rend(c)
#define ITR(i, b, e) for (auto i = (b); i != (e); ++i)
#define FORE(x, c) for (auto &x : c)
#define REPF(i, a, n) for (int i = a, i##len = (int)(n); i < i##len; ++i)
#define REP(i, n) REPF(i, 0, n)
#define REPR(i, n) for (int i = (int)(n); i >= 0; --i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c, x) (c.find(x) != end(c))
#define INSEG(l, x, r) ((l) <= (x) && (x) < (r))
#define dump(...)
#define pb push_back
#define _ 0
const signed INF_ = 1001001001;
const long long INF = 1001001001001001001LL;
const int DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
          DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0};
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  ITR(i, begin(v), end(v)) os << *i << (i == end(v) - 1 ? "" : " ");
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  ITR(i, begin(v), end(v)) is >> *i;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T, class U> bool chmax(T &a, const U &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T, class U> bool chmin(T &a, const U &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T> using heap = priority_queue<T, vector<T>, greater<T>>;
struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(15) << fixed;
#define endl "\n"
  }
} before_main_function;
//------------------8<------------------------------------8<--------------------

const int MOD = 1e9 + 7;
const int MAX_N = 200005;
int fact[MAX_N];
int fact_inv[MAX_N];
int bin_pow_mod(int x, int y) {
  if (x == 0)
    return 0;
  int prod = 1;
  while (y > 0) {
    if (y & 1)
      prod = (prod * x) % MOD;
    x = (x * x) % MOD;
    y >>= 1;
  }
  return prod % MOD;
}
struct init_fact {
  init_fact() {
    fact[0] = 1, fact_inv[0] = 1;
    for (int i = 1; i < MAX_N; ++i) {
      fact[i] = (fact[i - 1] * i) % MOD;
      fact_inv[i] = bin_pow_mod(fact[i], MOD - 2);
    }
  }
} init_fact;
int combination(int n, int r) {
  if (n < r)
    return 0;
  return ((fact[n] * fact_inv[n - r]) % MOD * fact_inv[r]) % MOD;
}

signed main() {
  int n;
  cin >> n;
  vi a(n + 1);
  cin >> a;

  map<int, int> cnt;
  FORE(x, a) cnt[x]++;

  int tmp = 0;
  vi cont;
  REP(i, n + 1) {
    if (cnt[a[i]] == 2) {
      cont.pb(tmp);
      tmp = 0;
    } else {
      tmp++;
    }
  }
  cont.pb(tmp);
  dump(cont);

  REPF(k, 1, n + 2) {
    int ans = combination(n + 1, k);
    REP(j, k) {
      // 左から j個, 右から k-j-1個
      ans -= combination(cont[0], j) * combination(cont[2], k - j - 1) % MOD;
      ans = (ans + MOD) % MOD;
    }
    cout << ans << endl;
  }
  return (0 ^ _ ^ 0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define ALL(c) begin(c), end(c)
#define RALL(c) rbegin(c), rend(c)
#define ITR(i, b, e) for (auto i = (b); i != (e); ++i)
#define FORE(x, c) for (auto &x : c)
#define REPF(i, a, n) for (int i = a, i##len = (int)(n); i < i##len; ++i)
#define REP(i, n) REPF(i, 0, n)
#define REPR(i, n) for (int i = (int)(n); i >= 0; --i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c, x) (c.find(x) != end(c))
#define INSEG(l, x, r) ((l) <= (x) && (x) < (r))
#define dump(...)
#define pb push_back
#define _ 0
const signed INF_ = 1001001001;
const long long INF = 1001001001001001001LL;
const int DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
          DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0};
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  ITR(i, begin(v), end(v)) os << *i << (i == end(v) - 1 ? "" : " ");
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  ITR(i, begin(v), end(v)) is >> *i;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T, class U> bool chmax(T &a, const U &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T, class U> bool chmin(T &a, const U &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T> using heap = priority_queue<T, vector<T>, greater<T>>;
struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(15) << fixed;
#define endl "\n"
  }
} before_main_function;
//------------------8<------------------------------------8<--------------------

const int MOD = 1e9 + 7;
const int MAX_N = 200005;
int fact[MAX_N];
int fact_inv[MAX_N];
int bin_pow_mod(int x, int y) {
  if (x == 0)
    return 0;
  int prod = 1;
  while (y > 0) {
    if (y & 1)
      prod = (prod * x) % MOD;
    x = (x * x) % MOD;
    y >>= 1;
  }
  return prod % MOD;
}
struct init_fact {
  init_fact() {
    fact[0] = 1, fact_inv[0] = 1;
    for (int i = 1; i < MAX_N; ++i) {
      fact[i] = (fact[i - 1] * i) % MOD;
      fact_inv[i] = bin_pow_mod(fact[i], MOD - 2);
    }
  }
} init_fact;
int combination(int n, int r) {
  if (n < r)
    return 0;
  return ((fact[n] * fact_inv[n - r]) % MOD * fact_inv[r]) % MOD;
}

signed main() {
  int n;
  cin >> n;
  vi a(n + 1);
  cin >> a;

  map<int, int> cnt;
  FORE(x, a) cnt[x]++;

  int tmp = 0;
  vi cont;
  REP(i, n + 1) {
    if (cnt[a[i]] == 2) {
      cont.pb(tmp);
      tmp = 0;
    } else {
      tmp++;
    }
  }
  cont.pb(tmp);
  dump(cont);

  REPF(k, 1, n + 2) {
    int ans = combination(n + 1, k);
    ans -= combination(cont[0] + cont[2], k - 1);
    ans = (ans + MOD) % MOD;
    cout << ans << endl;
  }
  return (0 ^ _ ^ 0);
}