Problem p03674 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mkp make_pair
#define fi first
#define se second
#define pr(num) cout << num << "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX 101010
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll> Map;

ll N;
ll a[MAX] = {};
ll cnt[MAX] = {};
ll id = -1;
ll l = -1, r = -1;
ll mfa[MAX];
ll mrfa[MAX];

ll mPow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}

ll fa(ll x) {
  if (x == 0)
    return 1;
  if (mfa[x] != -1)
    return mfa[x];
  return mfa[x] = x * fa(x - 1) % MOD;
}

ll rfa(ll x) {
  if (x == 0)
    return 1;
  if (mrfa[x] != -1)
    return mrfa[x];
  rfa(x - 1);
  return mrfa[x] = mPow(fa(x), MOD - 2) % MOD;
}

ll C(ll n, ll k) { return fa(n) * rfa(k) % MOD * rfa(n - k) % MOD; }

int main(void) {
  cin >> N;
  ll i;

  for (i = 1; i <= N + 1; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    if (cnt[a[i]] == 2)
      id = a[i];
  }

  for (i = 1; i <= N + 1; i++) {
    if (a[i] == id) {
      if (l == -1)
        l = i;
      else
        r = i;
    }
  }

  memset(mfa, -1, sizeof(mfa));
  memset(mrfa, -1, sizeof(mrfa));

  fa(100010);
  rfa(100010);

  for (i = 1; i <= N + 1; i++) {
    pr((C(N + 1, i) - C(N + l - r, i - 1) + MOD) % MOD);
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mkp make_pair
#define fi first
#define se second
#define pr(num) cout << num << "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX 101010
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll> Map;

ll N;
ll a[MAX] = {};
ll cnt[MAX] = {};
ll id = -1;
ll l = -1, r = -1;
ll mfa[MAX];
ll mrfa[MAX];

ll mPow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}

ll fa(ll x) {
  if (x == 0)
    return 1;
  if (mfa[x] != -1)
    return mfa[x];
  return mfa[x] = x * fa(x - 1) % MOD;
}

ll rfa(ll x) {
  if (x == 0)
    return 1;
  if (mrfa[x] != -1)
    return mrfa[x];
  rfa(x - 1);
  return mrfa[x] = mPow(fa(x), MOD - 2) % MOD;
}

ll C(ll n, ll k) {
  if (n < k)
    return 0;
  return fa(n) * rfa(k) % MOD * rfa(n - k) % MOD;
}

int main(void) {
  cin >> N;
  ll i;

  for (i = 1; i <= N + 1; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    if (cnt[a[i]] == 2)
      id = a[i];
  }

  for (i = 1; i <= N + 1; i++) {
    if (a[i] == id) {
      if (l == -1)
        l = i;
      else
        r = i;
    }
  }

  memset(mfa, -1, sizeof(mfa));
  memset(mrfa, -1, sizeof(mrfa));

  fa(100010);
  rfa(100010);

  for (i = 1; i <= N + 1; i++) {
    pr((C(N + 1, i) - C(N + l - r, i - 1) + MOD) % MOD);
  }
}