Problem p03674 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <iterator>
#include <queue>
#include <string>
#include <vector>

using namespace std;
const long long INF = 9223372036854775800;
const long long mod = 1e9 + 7;
const int MOD = 1e9 + 7;
long long fac[100001];
long long ifac[100001];
long long mod_pow(int x, int n) { // xのn乗
  if (n % 2 == 0) {
    if (n == 0) {
      return 1;
    } else {
      long long c = mod_pow(x, n / 2);
      return c * c % mod;
    }
  } else {
    if (n == 1) {
      return (long long)x;
    } else {
      long long c = mod_pow(x, (n - 1) / 2);
      return (c * c % mod) * (long long)x % mod;
    }
  }
}
long long comb(int a, int b) { // aCbを返す関数
  if (b == a || b == 0)
    return 1;
  if (a <= 0 || a < b)
    return 0;
  long long ret = (((fac[a] * ifac[a - b]) % mod) * ifac[b]) % mod;
  return ret;
}

int main() {
  int N;
  int A[100002];
  int left, right;
  int Where[100001] = {};
  cin >> N;
  for (int i = 1; i <= N + 1; i++) {
    cin >> A[i];
    if (Where[A[i]] == 0) {
      Where[A[i]] = i;
    } else {
      left = Where[A[i]];
      right = i;
    }
  }
  left--;
  right = N + 1 - right;
  fac[0] = 1;
  ifac[0] = 1;
  fac[1] = 1;
  ifac[1] = 1;
  for (int i = 2; i <= N + 1; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
    ifac[i] = (ifac[i - 1] * mod_pow(i, MOD - 2)) % mod;
  }
  for (int k = 1; k <= N + 1; k++) {
    long long ans = comb(N + 1, k);
    long long sub = 0;
    for (int j = 0; j < k; j++) {
      sub = (sub + comb(left, j) * comb(right, k - j - 1) % mod) % mod;
    }
    ans = (ans - sub + mod) % mod;
    //(j文字,(2回現れる文字),(k-j)文字)というパターンの引き算が残っていた
    if (k == 1)
      ans = N;
    if (k == N + 1)
      ans = 1;
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <queue>
#include <string>
#include <vector>

using namespace std;
const long long INF = 9223372036854775800;
const long long mod = 1e9 + 7;
const int MOD = 1e9 + 7;
long long fac[100001];
long long ifac[100001];
long long mod_pow(int x, int n) { // xのn乗
  if (n % 2 == 0) {
    if (n == 0) {
      return 1;
    } else {
      long long c = mod_pow(x, n / 2);
      return c * c % mod;
    }
  } else {
    if (n == 1) {
      return (long long)x;
    } else {
      long long c = mod_pow(x, (n - 1) / 2);
      return (c * c % mod) * (long long)x % mod;
    }
  }
}
long long comb(int a, int b) { // aCbを返す関数
  if (b == a || b == 0)
    return 1;
  if (a <= 0 || a < b)
    return 0;
  long long ret = (((fac[a] * ifac[a - b]) % mod) * ifac[b]) % mod;
  return ret;
}

int main() {
  int N;
  int A[100002];
  int left, right;
  int Where[100001] = {};
  cin >> N;
  for (int i = 1; i <= N + 1; i++) {
    cin >> A[i];
    if (Where[A[i]] == 0) {
      Where[A[i]] = i;
    } else {
      left = Where[A[i]];
      right = i;
    }
  }
  left--;
  right = N + 1 - right;
  fac[0] = 1;
  ifac[0] = 1;
  fac[1] = 1;
  ifac[1] = 1;
  for (int i = 2; i <= N + 1; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
    ifac[i] = (ifac[i - 1] * mod_pow(i, MOD - 2)) % mod;
  }
  for (int k = 1; k <= N + 1; k++) {
    long long ans = comb(N + 1, k);
    long long sub = comb(left + right, k - 1);
    /*
    for(int j=0;j<k;j++){
        sub = (sub+comb(left,j)*comb(right,k-j-1)%mod)%mod;
    }*/
    ans = (ans - sub + mod) % mod;
    //(j文字,(2回現れる文字),(k-j)文字)というパターンの引き算が残っていた
    if (k == 1)
      ans = N;
    if (k == N + 1)
      ans = 1;
    cout << ans << endl;
  }
  return 0;
}