Problem p03676 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template <class T> ostream &prnt(ostream &out, T v) {
  out << v.size() << '\n';
  for (auto e : v)
    out << e << ' ';
  return out;
}
template <class T> ostream &operator<<(ostream &out, vector<T> v) {
  return prnt(out, v);
}
template <class T> ostream &operator<<(ostream &out, set<T> v) {
  return prnt(out, v);
}
template <class T1, class T2> ostream &operator<<(ostream &out, map<T1, T2> v) {
  return prnt(out, v);
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> p) {
  return out << '(' << p.st << ' ' << p.nd << ')';
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n)                                                            \
  do {                                                                         \
    cerr << #x "[]: ";                                                         \
    for (long long _ = 0; _ < n; ++_)                                          \
      cerr << x[_] << " ";                                                     \
    cerr << '\n';                                                              \
  } while (0)
#define dbg_ok cerr << "OK!\n"

const long long N = 100100;
const long long MOD = 1e9 + 7;
long long n, a[N], ff[N];
long long f[N];

long long powMod(long long base, long long exp) {
  if (exp == 0)
    return 1;
  if (exp == 1)
    return base;
  long long tmp = powMod(base, exp / 2);
  tmp = (tmp * tmp) % MOD;
  if (exp % 2 == 1)
    tmp = (tmp * base) % MOD;
  return tmp;
}

long long invMod(long long nr) { return powMod(nr, MOD - 2); }

long long comb(long long k, long long n) {
  if (k > n)
    while (1)
      ;
  // dbg(k, n);
  // dbg(f[n]);
  // dbg((f[k] * f[n - k]));
  return (f[n] * invMod((f[k] * f[n - k]) % MOD)) % MOD;
}

int main() {
  ios_base::sync_with_stdio(false);

  cin >> n;

  f[0] = 1;
  for (long long i = 1; i <= n + 3; i++)
    f[i] = (f[i - 1] * i) % MOD;

  pair<long long, long long> pos;

  for (long long i = 1; i <= n + 1; i++) {
    cin >> a[i];
    // dbg(i, a[i]);
    if (ff[a[i]])
      pos = {ff[a[i]], i};
    else
      ff[a[i]] = i;
  }
  // dbg_v(ff, 4);
  long long l = pos.st;
  long long r = pos.nd;
  long long d = r - l + 1;
  // dbg(l, r);

  for (long long i = 1; i <= n + 1; i++) {
    long long ans = comb(i, n + 1);
    // dbg(ans);
    ans -= comb(i - 1, n + 1 - d);
    ans %= MOD;
    if (ans < 0)
      ans += MOD;
    cout << ans << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template <class T> ostream &prnt(ostream &out, T v) {
  out << v.size() << '\n';
  for (auto e : v)
    out << e << ' ';
  return out;
}
template <class T> ostream &operator<<(ostream &out, vector<T> v) {
  return prnt(out, v);
}
template <class T> ostream &operator<<(ostream &out, set<T> v) {
  return prnt(out, v);
}
template <class T1, class T2> ostream &operator<<(ostream &out, map<T1, T2> v) {
  return prnt(out, v);
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> p) {
  return out << '(' << p.st << ' ' << p.nd << ')';
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n)                                                            \
  do {                                                                         \
    cerr << #x "[]: ";                                                         \
    for (long long _ = 0; _ < n; ++_)                                          \
      cerr << x[_] << " ";                                                     \
    cerr << '\n';                                                              \
  } while (0)
#define dbg_ok cerr << "OK!\n"

const long long N = 100100;
const long long MOD = 1e9 + 7;
long long n, a[N], ff[N];
long long f[N];

long long powMod(long long base, long long exp) {
  if (exp == 0)
    return 1;
  if (exp == 1)
    return base;
  long long tmp = powMod(base, exp / 2);
  tmp = (tmp * tmp) % MOD;
  if (exp % 2 == 1)
    tmp = (tmp * base) % MOD;
  return tmp;
}

long long invMod(long long nr) { return powMod(nr, MOD - 2); }

long long comb(long long k, long long n) {
  if (k > n)
    return 0;
  // dbg(k, n);
  // dbg(f[n]);
  // dbg((f[k] * f[n - k]));
  return (f[n] * invMod((f[k] * f[n - k]) % MOD)) % MOD;
}

int main() {
  ios_base::sync_with_stdio(false);

  cin >> n;

  f[0] = 1;
  for (long long i = 1; i <= n + 3; i++)
    f[i] = (f[i - 1] * i) % MOD;

  pair<long long, long long> pos;

  for (long long i = 1; i <= n + 1; i++) {
    cin >> a[i];
    // dbg(i, a[i]);
    if (ff[a[i]])
      pos = {ff[a[i]], i};
    else
      ff[a[i]] = i;
  }
  // dbg_v(ff, 4);
  long long l = pos.st;
  long long r = pos.nd;
  long long d = r - l + 1;
  // dbg(l, r);

  for (long long i = 1; i <= n + 1; i++) {
    long long ans = comb(i, n + 1);
    // dbg(ans);
    ans -= comb(i - 1, n + 1 - d);
    ans %= MOD;
    if (ans < 0)
      ans += MOD;
    cout << ans << '\n';
  }
}