Problem p03674 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define ss(n) scanf("%s", &n)
#define MOD 1000000007
#define ll long long int
#define vi vector<int>
#define vii vector<vector<int>>
#define PI 3.1415926535897932384626433832795

int facts[100005];
int factinvs[100005];

int gcdExtended(
    int a, int b, int *x,
    int *
        y) { // From
             // http://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }

  int x1, y1; // To store results of recursive call
  int gcd = gcdExtended(b % a, a, &x1, &y1);

  // Update x and y using results of recursive
  // call
  *x = y1 - (b / a) * x1;
  *y = x1;

  return gcd;
}

int modinv(int a) {
  int x, y;
  gcdExtended(a, MOD, &x, &y);
  return (x % MOD + MOD) % MOD;
}

void genfacts() {
  ll num = 1;
  facts[0] = 1;
  factinvs[0] = 1;
  for (int i = 1; i <= 100003; i++) {
    num *= i;
    num = num % MOD;
    facts[i] = num;
    factinvs[i] = modinv(num);
  }
}

ll binom(int n, int k) {
  ll a = facts[n];
  ll b = factinvs[n - k];
  ll c = factinvs[k];
  return ((a * b) % MOD * c) % MOD;
}

int main() {
  int n;
  cin >> n;
  genfacts();
  vector<int> wasat(n + 1, -1);
  int d = 0;
  for (int i = 0; i < n + 1; i++) {
    int x;
    cin >> x;
    if (wasat[x] != -1) {
      d = i - wasat[x];
      break;
    } else {
      wasat[x] = i;
    }
  }
  for (int i = 1; i <= n + 1; i++) {
    cout << (binom(n + 1, i) - binom(n - d, i - 1) + MOD) % MOD << "\n";
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define ss(n) scanf("%s", &n)
#define MOD 1000000007
#define ll long long int
#define vi vector<int>
#define vii vector<vector<int>>
#define PI 3.1415926535897932384626433832795

int facts[100005];
int factinvs[100005];

int gcdExtended(
    int a, int b, int *x,
    int *
        y) { // From
             // http://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }

  int x1, y1; // To store results of recursive call
  int gcd = gcdExtended(b % a, a, &x1, &y1);

  // Update x and y using results of recursive
  // call
  *x = y1 - (b / a) * x1;
  *y = x1;

  return gcd;
}

int modinv(int a) {
  int x, y;
  gcdExtended(a, MOD, &x, &y);
  return (x % MOD + MOD) % MOD;
}

void genfacts() {
  ll num = 1;
  facts[0] = 1;
  factinvs[0] = 1;
  for (int i = 1; i <= 100003; i++) {
    num *= i;
    num = num % MOD;
    facts[i] = num;
    factinvs[i] = modinv(num);
  }
}

ll binom(int n, int k) {
  if (k > n) {
    return 0;
  }
  ll a = facts[n];
  ll b = factinvs[n - k];
  ll c = factinvs[k];
  return ((a * b) % MOD * c) % MOD;
}

int main() {
  int n;
  cin >> n;
  genfacts();
  vector<int> wasat(n + 1, -1);
  int d = 0;
  for (int i = 0; i < n + 1; i++) {
    int x;
    cin >> x;
    if (wasat[x] != -1) {
      d = i - wasat[x];
      break;
    } else {
      wasat[x] = i;
    }
  }
  for (int i = 1; i <= n + 1; i++) {
    cout << (binom(n + 1, i) - binom(n - d, i - 1) + MOD) % MOD << "\n";
  }
  return 0;
}