Problem p03676 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define ull unsigned ll
const ll mod = 1000000007ll;
int n, a[100005];
int used[100005];
ll fact[100005], dfact[100005];
ll pw(ll xx, int val = mod - 2) {
  if (!val)
    return 1ll;
  if (val & 1)
    return (xx * pw(xx, val - 1)) % mod;
  ll haha = pw(xx, val >> 1);
  return (haha * haha) % mod;
}
void init() {
  fact[0] = 1ll;
  dfact[0] = pw(fact[0]);
  for (int i = 1; i <= 100001; i++) {
    fact[i] = ((ll)fact[i - 1] * (ll)i) % mod;
    dfact[i] = pw(fact[i]);
  }
}
ll C(int i, int j) {
  if (i < j)
    return 0ll;
  ll ret;
  ret = ((((ll)fact[i] * (ll)dfact[j]) % mod) * (ll)dfact[i - j]) % mod;
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int x1, x2;
  cin >> n;
  init();
  memset(used, -1, sizeof(used));
  for (int i = 0; i < n + 1; i++) {
    cin >> a[i];
    if (used[a[i]] != -1) {
      x1 = used[a[i]] + 1;
      x2 = i + 1;
    }
    used[a[i]] = i;
  }
  for (int i = 1; i <= n + 1; i++) {
    if (i == 1)
      cout << n << endl;
    else if (i == n + 1)
      cout << 1 << endl;
    else {
      ll tmp = C(n + 1, i);
      for (int j = 0; j <= i - 1; j++) {
        tmp = (tmp - (((ll)C(n + 1 - x2, j) * (ll)C(x1 - 1, i - 1 - j)) % mod) +
               mod) %
              mod;
      }
      cout << tmp << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define ull unsigned ll
const ll mod = 1000000007ll;
int n, a[100005];
int used[100005];
ll fact[100005], dfact[100005];
ll pw(ll xx, int val = mod - 2) {
  if (!val)
    return 1ll;
  if (val & 1)
    return (xx * pw(xx, val - 1)) % mod;
  ll haha = pw(xx, val >> 1);
  return (haha * haha) % mod;
}
void init() {
  fact[0] = 1ll;
  dfact[0] = pw(fact[0]);
  for (int i = 1; i <= 100001; i++) {
    fact[i] = ((ll)fact[i - 1] * (ll)i) % mod;
    dfact[i] = pw(fact[i]);
  }
}
ll C(int i, int j) {
  if (i < j)
    return 0ll;
  ll ret;
  ret = ((((ll)fact[i] * (ll)dfact[j]) % mod) * (ll)dfact[i - j]) % mod;
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int x1, x2;
  cin >> n;
  init();
  memset(used, -1, sizeof(used));
  for (int i = 0; i < n + 1; i++) {
    cin >> a[i];
    if (used[a[i]] != -1) {
      x1 = used[a[i]] + 1;
      x2 = i + 1;
    }
    used[a[i]] = i;
  }
  for (int i = 1; i <= n + 1; i++) {
    if (i == 1)
      cout << n << endl;
    else if (i == n + 1)
      cout << 1 << endl;
    else {
      ll tmp = C(n + 1, i);
      tmp -= C(x1 + n - x2, i - 1);
      tmp = (tmp + mod) % mod;
      cout << tmp << endl;
    }
  }
  return 0;
}