Problem p03676 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;
ll mod = 1e9 + 7;
ll inf = 1e18;
// LLONG_MIN

struct Factorial {
  long long N, mod;
  vector<long long> fact, inv;
  long long mod_pow(long long x, long long n) {
    long long res = 1;
    while (n) {
      if (n & 1)
        (res *= x) %= mod;
      (x *= x) %= mod;
      n >>= 1;
    }
    return res;
  }
  Factorial() {}
  Factorial(long long sn, long long smod) {
    N = sn;
    mod = smod;
    fact.reserve(sn + 1);
    inv.reserve(sn + 1);
    // 初期化
    fact[0] = 1;
    for (int i = 1; i <= sn; i++) {
      fact[i] = (fact[i - 1] * i) % mod;
    }
    inv[sn] = mod_pow(fact[sn], mod - 2);
    for (int i = sn; i > 0; i--) {
      inv[i - 1] = (inv[i] * i) % mod;
    }
  }
  long long Fact(long long n) {
    if (n < 0)
      return 0;
    return fact[n];
  }
  long long Inv(long long n) {
    if (n < 0)
      return 0;
    return inv[n];
  }
  long long Combination(long long n, long long r) {
    if (n < r)
      return 0;
    if (r < 0)
      return 0;
    long long ans = 1;
    ans = (ans * Fact(n)) % mod;
    ans = (ans * Inv(r)) % mod;
    ans = (ans * Inv(n - r)) % mod;
    return ans;
  }
};

int main(void) {
  ll n, a[100010], b[100010] = {}, l, r, L, R;
  cin >> n;
  reg(i, 1, n + 1) {
    cin >> a[i];
    if (b[a[i]] == 0) {
      b[a[i]] = i; // 場所記録
    } else {
      l = b[a[i]];
      r = i;
    }
  }
  L = l - 1;
  R = n + 1 - r;
  Factorial f(n + 1, mod);
  reg(k, 1, n + 1) {
    ll ans = f.Combination(n + 1, k);
    ll sum = 0;
    reg(i, 0, k - 1) {
      ll prod = (f.Combination(L, i) * f.Combination(R, k - 1 - i)) % mod;
      sum = (sum + prod) % mod;
    }
    ans = (ans + mod - sum) % mod;
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;
typedef pair<ll, ll> mp;
ll mod = 1e9 + 7;
ll inf = 1e18;
// LLONG_MIN

struct Factorial {
  long long N, mod;
  vector<long long> fact, inv;
  long long mod_pow(long long x, long long n) {
    long long res = 1;
    while (n) {
      if (n & 1)
        (res *= x) %= mod;
      (x *= x) %= mod;
      n >>= 1;
    }
    return res;
  }
  Factorial() {}
  Factorial(long long sn, long long smod) {
    N = sn;
    mod = smod;
    fact.reserve(sn + 1);
    inv.reserve(sn + 1);
    // 初期化
    fact[0] = 1;
    for (int i = 1; i <= sn; i++) {
      fact[i] = (fact[i - 1] * i) % mod;
    }
    inv[sn] = mod_pow(fact[sn], mod - 2);
    for (int i = sn; i > 0; i--) {
      inv[i - 1] = (inv[i] * i) % mod;
    }
  }
  long long Fact(long long n) {
    if (n < 0)
      return 0;
    return fact[n];
  }
  long long Inv(long long n) {
    if (n < 0)
      return 0;
    return inv[n];
  }
  long long Combination(long long n, long long r) {
    if (n < r)
      return 0;
    if (r < 0)
      return 0;
    long long ans = 1;
    ans = (ans * Fact(n)) % mod;
    ans = (ans * Inv(r)) % mod;
    ans = (ans * Inv(n - r)) % mod;
    return ans;
  }
};

int main(void) {
  ll n, a[100010], b[100010] = {}, l, r, L, R;
  cin >> n;
  reg(i, 1, n + 1) {
    cin >> a[i];
    if (b[a[i]] == 0) {
      b[a[i]] = i; // 場所記録
    } else {
      l = b[a[i]];
      r = i;
    }
  }
  L = l - 1;
  R = n + 1 - r;
  Factorial f(n + 1, mod);
  reg(k, 1, n + 1) {
    ll ans =
        (f.Combination(n + 1, k) + mod - f.Combination(L + R, k - 1)) % mod;
    cout << ans << endl;
  }
  return 0;
}