Problem p03676 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x, y) make_pair(x, y)
#define all(v) v.begin(), v.end()
#define pb(x) push_back(x)
#define wr cout << "----------------" << endl;
#define ppb() pop_back()
#define tr(ii, c)                                                              \
  for (__typeof((c).begin()) ii = (c).begin(); ii != (c).end(); ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr << #x << " = " << x << endl;
using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
template <class T> bool umin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool umax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int F[MAXN], inv[MAXN], vis[MAXN];
int mod(ll x) {
  while (x < 0)
    x += INF;
  return (x % INF);
}
int Fe(int x, int y) {
  if (!y)
    return 1;
  int h = Fe(x, y / 2);
  h = mod(h * 1LL * h);
  if (y & 1)
    h = mod(h * 1LL * x);
  return h;
}
int C(int x, int y) {
  if (x < y)
    return 0;
  return mod(F[x] * 1LL * mod(inv[y] * 1LL * inv[x - y]));
}
int main() {
  //~ #ifndef ONLINE_JUDGE
  //~ freopen("file.in", "r", stdin);
  //~ #endif
  F[0] = inv[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    F[i] = mod(F[i - 1] * 1LL * i);
    inv[i] = Fe(F[i], INF - 2);
  }
  int n, one, two;
  scanf("%d", &n);
  for (int i = 1; i <= n + 1; i++) {
    int x;
    scanf("%d", &x);
    if (vis[x])
      one = vis[x] - 1, two = n + 1 - i;
    vis[x] = i;
  }
  for (int i = 1; i <= n + 1; i++) {
    int res = C(n + 1, i);
    for (int j = max(0, i - two - 1); j <= min(i, one); j++)
      res = mod(res - mod(C(one, j) * 1LL * C(two, i - j - 1)));
    printf("%d\n", res);
  }
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x, y) make_pair(x, y)
#define all(v) v.begin(), v.end()
#define pb(x) push_back(x)
#define wr cout << "----------------" << endl;
#define ppb() pop_back()
#define tr(ii, c)                                                              \
  for (__typeof((c).begin()) ii = (c).begin(); ii != (c).end(); ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr << #x << " = " << x << endl;
using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
template <class T> bool umin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool umax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int F[MAXN], inv[MAXN], vis[MAXN];
int mod(ll x) {
  while (x < 0)
    x += INF;
  return (x % INF);
}
int Fe(int x, int y) {
  if (!y)
    return 1;
  int h = Fe(x, y / 2);
  h = mod(h * 1LL * h);
  if (y & 1)
    h = mod(h * 1LL * x);
  return h;
}
int C(int x, int y) {
  if (x < y)
    return 0;
  return mod(F[x] * 1LL * mod(inv[y] * 1LL * inv[x - y]));
}
int main() {
  //~ #ifndef ONLINE_JUDGE
  //~ freopen("file.in", "r", stdin);
  //~ #endif
  F[0] = inv[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    F[i] = mod(F[i - 1] * 1LL * i);
    inv[i] = Fe(F[i], INF - 2);
  }
  int n, one, two;
  scanf("%d", &n);
  for (int i = 1; i <= n + 1; i++) {
    int x;
    scanf("%d", &x);
    if (vis[x])
      one = vis[x] - 1, two = n + 1 - i;
    vis[x] = i;
  }
  for (int i = 1; i <= n + 1; i++)
    printf("%d\n", mod(C(n + 1, i) - C(one + two, i - 1)));
  return 0;
}