Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
const long long MOD = 1000000007;
const int MAX = 200001;
long long fac[MAX];
long long finv[MAX];
long long inv[MAX];

int BIT_SIZE = 64;
long long power(long long m, long long n) {
  long long ans = 1;
  long long tmp = m;
  for (int i = 0; i < BIT_SIZE; i++) {
    // cout << ans << endl;
    if (((n >> i) & 1) == 1) {
      ans = ans * tmp % MOD;
    }
    tmp = (tmp * tmp) % MOD;
  }
  return ans;
}

void comb_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

long long a[100001];
int appeared_in[100000];
int main() {
  comb_init();
  int n;
  cin >> n;
  for (int i = 0; i < n + 1; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n + 1; i++) {
    appeared_in[i] = -1;
  }

  int first_appearence;
  int second_appearence;
  for (int i = 0; i < n + 1; i++) {
    if (appeared_in[a[i] - 1] < 0) {
      appeared_in[a[i] - 1] = i;
    } else {
      first_appearence = appeared_in[a[i] - 1];
      second_appearence = i;
    }
  }
  // cout << first_appearence << " " << second_appearence << endl;
  for (int k = 1; k <= n + 1; k++) {
    long long all = comb(n + 1, k);
    long long double_count = 0;
    if (k - 1 <= first_appearence + n - second_appearence) {
      double_count = comb(first_appearence + n - second_appearence, k - 1);
    }
    // cout << all << " " << double_count << endl;
    cout << ((all - double_count) % MOD + MOD) % MOD << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
const long long MOD = 1000000007;
const int MAX = 200001;
long long fac[MAX];
long long finv[MAX];
long long inv[MAX];

int BIT_SIZE = 64;
long long power(long long m, long long n) {
  long long ans = 1;
  long long tmp = m;
  for (int i = 0; i < BIT_SIZE; i++) {
    // cout << ans << endl;
    if (((n >> i) & 1) == 1) {
      ans = ans * tmp % MOD;
    }
    tmp = (tmp * tmp) % MOD;
  }
  return ans;
}

void comb_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

long long a[100001];
int appeared_in[100001];
int main() {
  comb_init();
  int n;
  cin >> n;
  for (int i = 0; i < n + 1; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n + 1; i++) {
    appeared_in[i] = -1;
  }

  int first_appearence;
  int second_appearence;
  for (int i = 0; i < n + 1; i++) {
    if (appeared_in[a[i] - 1] < 0) {
      appeared_in[a[i] - 1] = i;
    } else {
      first_appearence = appeared_in[a[i] - 1];
      second_appearence = i;
    }
  }
  // cout << first_appearence << " " << second_appearence << endl;
  for (int k = 1; k <= n + 1; k++) {
    long long all = comb(n + 1, k);
    long long double_count = 0;
    if (k - 1 <= first_appearence + n - second_appearence) {
      double_count = comb(first_appearence + n - second_appearence, k - 1);
    }
    // cout << all << " " << double_count << endl;
    cout << ((all - double_count) % MOD + MOD) % MOD << endl;
  }
  return 0;
}