Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VS = vector<string>;
using PII = pair<int, int>;
using VPII = vector<PII>;
using VL = vector<long long>;
using VVL = vector<VL>;

#define sz(var) (int)var.size()
#define REP(var, start, end) for (int var = start; var < (int)end; ++var)
#define CLEAR(var, with) memset(var, with, sizeof(var))
#define ALL(x) x.begin(), x.end()
#define LL long long
#define ULL unsigned long long
#define TIME                                                                   \
  cout << "Time elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << endl;

const int MOD = (int)1e9 + 7, MAXN = (int)50;
int n;
VI A;

int mul(int a, int b) { return (1ll * a * b) % MOD; }

int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + MOD : a;
}

int fact[MAXN], finv[MAXN], inv[MAXN];
void precalc() {
  fact[0] = fact[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (LL i = 2; i < MAXN; ++i) {
    inv[i] = sub(MOD, mul(inv[MOD % i], MOD / i));
    fact[i] = mul(fact[i - 1], i);
    finv[i] = mul(finv[i - 1], inv[i]);
  }
}

int comb(int n, int r) {
  if (n < r)
    return 0;
  return mul(fact[n], mul(finv[r], finv[n - r]));
}

void solve() {
  precalc();

  int dist;
  VI pos(MAXN + 1, -1);
  for (int i = 0; i < n + 1; ++i) {
    if (pos[A[i]] != -1) {
      dist = i - pos[A[i]];
      break;
    }
    pos[A[i]] = i;
  }

  for (int len = 1; len <= n + 1; ++len) {
    int whole = comb(n + 1, len), remain = comb(n - dist, len - 1);
    cout << sub(whole, remain) << '\n';
  }
  cout << flush;
}

int main() {
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
//	freopen("tmp.in", "r", stdin);
//	freopen("tmp.out", "w", stderr);
#endif
  cin >> n;

  A = VI(n + 1, 0);
  REP(i, 0, n + 1) cin >> A[i];

  solve();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VS = vector<string>;
using PII = pair<int, int>;
using VPII = vector<PII>;
using VL = vector<long long>;
using VVL = vector<VL>;

#define sz(var) (int)var.size()
#define REP(var, start, end) for (int var = start; var < (int)end; ++var)
#define CLEAR(var, with) memset(var, with, sizeof(var))
#define ALL(x) x.begin(), x.end()
#define LL long long
#define ULL unsigned long long
#define TIME                                                                   \
  cout << "Time elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << endl;

const int MOD = (int)1e9 + 7, MAXN = (int)1e5 + 10;
int n;
VI A;

int mul(int a, int b) { return (1ll * a * b) % MOD; }

int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + MOD : a;
}

int fact[MAXN], finv[MAXN], inv[MAXN];
void precalc() {
  fact[0] = fact[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (LL i = 2; i < MAXN; ++i) {
    inv[i] = sub(MOD, mul(inv[MOD % i], MOD / i));
    fact[i] = mul(fact[i - 1], i);
    finv[i] = mul(finv[i - 1], inv[i]);
  }
}

int comb(int n, int r) {
  if (n < r)
    return 0;
  return mul(fact[n], mul(finv[r], finv[n - r]));
}

void solve() {
  precalc();

  int dist;
  VI pos(MAXN + 1, -1);
  for (int i = 0; i < n + 1; ++i) {
    if (pos[A[i]] != -1) {
      dist = i - pos[A[i]];
      break;
    }
    pos[A[i]] = i;
  }

  for (int len = 1; len <= n + 1; ++len) {
    int whole = comb(n + 1, len), remain = comb(n - dist, len - 1);
    cout << sub(whole, remain) << '\n';
  }
  cout << flush;
}

int main() {
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
//	freopen("tmp.in", "r", stdin);
//	freopen("tmp.out", "w", stderr);
#endif
  cin >> n;

  A = VI(n + 1, 0);
  REP(i, 0, n + 1) cin >> A[i];

  solve();

  return 0;
}