Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <iomanip>
#include <iostream>
// #include<cstdio>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int Nmax = 100000;
const ll P = 1000000007;
int N;
int A[Nmax + 1];
ll fact[3 * Nmax];

ll ext_gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll nx, ny;
  ll g = ext_gcd(b, a % b, nx, ny);
  x = ny;
  y = nx - a / b * ny;
  return g;
}

ll inv_mod(ll x, ll p) {
  ll y, z;
  ext_gcd(x, p, y, z);
  while (y < 0)
    y += p;
  return y;
}

ll fact_mod(ll n, ll p, ll &e) {
  // n! = a * p^e
  // return = a % p
  if (n == 0) {
    e = 0;
    return 1;
  }

  ll na = fact_mod(n / p, p, e);

  e += n / p;
  ll a = na * fact[n % p] % p;

  if ((n / p) % 2)
    a = a * (p - 1) % p;
  return a;
}

ll comb_mod(ll n, ll m, ll p) {
  ll e1, e2, e3;
  ll a1 = fact_mod(n, p, e1);
  ll a2 = fact_mod(m, p, e2);
  ll a3 = fact_mod(n - m, p, e3);

  if (e1 > e2 + e3)
    return 0;
  else
    return a1 * inv_mod(a2 * a3 % p, p) % p;
}

int main() {
  cin >> N;
  for (int i = 0; i <= N; i++) {
    cin >> A[i];
    A[i]--;
  }

  fact[0] = fact[1] = 1;
  for (int i = 2; i < 3 * Nmax; i++)
    fact[i] = fact[i - 1] * i % P;

  vector<int> appear(N + 1, -1);
  int dupi, dupj;
  for (int i = 0; i <= N; i++) {
    if (appear[A[i]] >= 0) {
      dupi = appear[A[i]];
      dupj = i;
      break;
    }
    appear[A[i]] = i;
  }

  if (N == 1) {
    cout << 1 << endl;
    cout << 1 << endl;
    return 0;
  }

  for (int i = 0; i <= N; i++) {
    cout << (comb_mod(N + 1, i + 1, P) - comb_mod(N + dupi - dupj, i, P) + P) %
                P
         << endl;
  }

  return 0;
}


Fixed submission:
#include <iomanip>
#include <iostream>
// #include<cstdio>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int Nmax = 100000;
const ll P = 1000000007;
int N;
int A[Nmax + 1];
ll fact[3 * Nmax];

ll ext_gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll nx, ny;
  ll g = ext_gcd(b, a % b, nx, ny);
  x = ny;
  y = nx - a / b * ny;
  return g;
}

ll inv_mod(ll x, ll p) {
  ll y, z;
  ext_gcd(x, p, y, z);
  while (y < 0)
    y += p;
  return y;
}

ll fact_mod(ll n, ll p, ll &e) {
  // n! = a * p^e
  // return = a % p
  if (n == 0) {
    e = 0;
    return 1;
  }

  ll na = fact_mod(n / p, p, e);

  e += n / p;
  ll a = na * fact[n % p] % p;

  if ((n / p) % 2)
    a = a * (p - 1) % p;
  return a;
}

ll comb_mod(ll n, ll m, ll p) {
  ll e1, e2, e3;
  ll a1 = fact_mod(n, p, e1);
  ll a2 = fact_mod(m, p, e2);
  ll a3 = fact_mod(n - m, p, e3);

  if (e1 > e2 + e3)
    return 0;
  else
    return a1 * inv_mod(a2 * a3 % p, p) % p;
}

int main() {
  cin >> N;
  for (int i = 0; i <= N; i++) {
    cin >> A[i];
    A[i]--;
  }

  fact[0] = fact[1] = 1;
  for (int i = 2; i < 3 * Nmax; i++)
    fact[i] = fact[i - 1] * i % P;

  vector<int> appear(N + 1, -1);
  int dupi, dupj;
  for (int i = 0; i <= N; i++) {
    if (appear[A[i]] >= 0) {
      dupi = appear[A[i]];
      dupj = i;
      break;
    }
    appear[A[i]] = i;
  }

  if (N == 1) {
    cout << 1 << endl;
    cout << 1 << endl;
    return 0;
  }

  for (int i = 0; i <= N; i++) {
    if (i > N + dupi - dupj) {
      cout << comb_mod(N + 1, i + 1, P) << endl;
    } else {
      cout << (comb_mod(N + 1, i + 1, P) - comb_mod(N + dupi - dupj, i, P) +
               P) %
                  P
           << endl;
    }
  }

  return 0;
}