Problem p03676 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define INFI 987654321
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define MOD 1000000007
int powi(int a, int b, int mod) {
  long long res = 1;
  long long x, y;
  y = 1;
  x = a % mod;
  while (y <= b) {
    if ((b & y) == y) {
      res *= x;
      res %= mod;
    }
    x *= x;
    y *= 2;
    x %= mod;
  }
  return res;
}

/*
 * mod가 소수이고, div가 mod의 배수가 아니어야 함
 */
int get_mod_inverse(int div, int mod) { return powi(div, mod - 2, mod); }

int cache[100005];
int factorial(int num) {
  if (num <= 1)
    return 1;
  if (cache[num])
    return cache[num];
  return cache[num] = ((ll)factorial(num - 1) * num) % MOD;
}

bool check[100005];
int main() {
  ios_base::sync_with_stdio(false);
  int N, dup_idx;
  vector<int> vec;
  cin >> N;
  for (int i = 0; i <= N; ++i) {
    int here;
    cin >> here;
    vec.push_back(here);
    if (check[here]) {
      dup_idx = i;
    }
    check[here] = 1;
  }
  int dup2;
  for (int i = 0; i <= N; ++i) {
    if (vec[i] == vec[dup_idx]) {
      dup2 = i;
      break;
    }
  }

  for (int len = 1; len <= N + 1; ++len) {
    ll ans = factorial(N + 1);
    ans = (ans * get_mod_inverse(factorial(len), MOD)) % MOD;
    ans = (ans * get_mod_inverse(factorial(N + 1 - len), MOD)) % MOD;
    int lc = dup2;
    int rc = N - dup_idx;
    for (int i = 0; i < len; ++i) {
      int ln = i;
      int rn = len - i - 1;
      if (ln > lc)
        continue;
      if (rn > rc)
        continue;
      ll sub = factorial(lc);
      sub = (sub * get_mod_inverse(factorial(ln), MOD)) % MOD;
      sub = (sub * get_mod_inverse(factorial(lc - ln), MOD)) % MOD;
      sub = (sub * factorial(rc)) % MOD;
      sub = (sub * get_mod_inverse(factorial(rn), MOD)) % MOD;
      sub = (sub * get_mod_inverse(factorial(rc - rn), MOD)) % MOD;
      ans = (ans + MOD - sub) % MOD;
    }
    printf("%lld\n", ans);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define INFI 987654321
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define MOD 1000000007
int powi(int a, int b, int mod) {
  long long res = 1;
  long long x, y;
  y = 1;
  x = a % mod;
  while (y <= b) {
    if ((b & y) == y) {
      res *= x;
      res %= mod;
    }
    x *= x;
    y *= 2;
    x %= mod;
  }
  return res;
}

/*
 * mod가 소수이고, div가 mod의 배수가 아니어야 함
 */
int get_mod_inverse(int div, int mod) { return powi(div, mod - 2, mod); }

int cache[100005];
int factorial(int num) {
  if (num <= 1)
    return 1;
  if (cache[num])
    return cache[num];
  return cache[num] = ((ll)factorial(num - 1) * num) % MOD;
}

bool check[100005];
int main() {
  ios_base::sync_with_stdio(false);
  int N, dup_idx;
  vector<int> vec;
  cin >> N;
  for (int i = 0; i <= N; ++i) {
    int here;
    cin >> here;
    vec.push_back(here);
    if (check[here]) {
      dup_idx = i;
    }
    check[here] = 1;
  }
  int dup2;
  for (int i = 0; i <= N; ++i) {
    if (vec[i] == vec[dup_idx]) {
      dup2 = i;
      break;
    }
  }

  for (int len = 1; len <= N + 1; ++len) {
    ll ans = factorial(N + 1);
    ans = (ans * get_mod_inverse(factorial(len), MOD)) % MOD;
    ans = (ans * get_mod_inverse(factorial(N + 1 - len), MOD)) % MOD;
    int lc = dup2;
    int rc = N - dup_idx;
    int totc = lc + rc;
    if (totc >= len - 1) {
      ll sub = factorial(totc);
      sub = (sub * get_mod_inverse(factorial(len - 1), MOD)) % MOD;
      sub = (sub * get_mod_inverse(factorial(totc - len + 1), MOD)) % MOD;
      ans = (ans + MOD - sub) % MOD;
    }
    printf("%lld\n", ans);
  }

  return 0;
}