Problem p03674 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

#define INF (1 << 30)
// 1,073,741,824
//= 536,870,912 *2

#define MOD 1000000007

// lower_bound(A.begin(), A.end(), N)
// upper_bound(...

// A.erase(unique(A.begin(), A.end()), A.end())

// bit: &/and, |/or, ^/xor, ~/not

// getline(cin, String)
// while (getline(cin, S)) {}

// cout <<fixed <<setprecision(10)

// priority_queue<int> q;
// priority_queue<int, vector<int>, less<int>> q; // Default: vector, less
// priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;

// Graph
// Warshall-Floyd: Distance between each node, N^3
// Dijkstra: Distance from the start node, N^2
// DFS(Depth-First Search)

//
// Notes
//
// When specified less than N, try full scan.
// RE: empty vector
// Check Qs
// long/int
// xor -> count each bit
// Check values
// 	1,000,000 -> Loop
// 	100,000	-> vector(int:400KB, long:800KB)

const int MAX = 100001;

void ComInit(vector<long> &fac, vector<long> &finv, vector<long> &inv) {
  fac.at(0) = 1;
  fac.at(1) = 1;
  finv.at(0) = 1;
  finv.at(1) = 1;
  inv.at(1) = 1;
  for (int i = 2; i < MAX; i++) {
    fac.at(i) = (fac.at(i - 1) * i) % MOD;
    inv.at(i) = MOD - (inv.at(MOD % i) * (MOD / i)) % MOD;
    finv.at(i) = (finv.at(i - 1) * inv.at(i)) % MOD;
  }
}

long COM(int N, int K, vector<long> &fac, vector<long> &finv) {
  if (N < K)
    return 0;
  if ((N < 0) || (K < 0))
    return 0;
  return (fac.at(N) * ((finv.at(K) * finv.at(N - K)) % MOD)) % MOD;
}

int main() {
  // Array
  vector<long> fac(MAX);
  vector<long> finv(MAX);
  vector<long> inv(MAX);
  // Initialize
  ComInit(fac, finv, inv);
  /*
  // Example
  cout << "N?";
  int i;
  cin >>i;
  for (int j=0; j<=i; j++)
          cout << i << 'C' << j << " = " << COM(i, j, fac, finv) << endl;
  */
  long n;
  cin >> n;
  vector<long> a(n + 2, 0);
  vector<long> b(n + 1, 0);
  long l = 0;
  long r = 0;
  for (int i = 1; i <= n + 1; i++) {
    cin >> a.at(i);
    if (0 == b.at(a.at(i)))
      b.at(a.at(i)) = i;
    else {
      l = b.at(a.at(i));
      r = i;
    }
  }
  for (int long i = 1; i <= n + 1; i++) {
    long S = COM(n + 1, i, fac, finv);
    S += MOD - COM(n - r + l, i - 1, fac, finv);
    S %= MOD;
    cout << S << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

#define INF (1 << 30)
// 1,073,741,824
//= 536,870,912 *2

#define MOD 1000000007

// lower_bound(A.begin(), A.end(), N)
// upper_bound(...

// A.erase(unique(A.begin(), A.end()), A.end())

// bit: &/and, |/or, ^/xor, ~/not

// getline(cin, String)
// while (getline(cin, S)) {}

// cout <<fixed <<setprecision(10)

// priority_queue<int> q;
// priority_queue<int, vector<int>, less<int>> q; // Default: vector, less
// priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;

// Graph
// Warshall-Floyd: Distance between each node, N^3
// Dijkstra: Distance from the start node, N^2
// DFS(Depth-First Search)

//
// Notes
//
// When specified less than N, try full scan.
// RE: empty vector
// Check Qs
// long/int
// xor -> count each bit
// Check values
// 	1,000,000 -> Loop
// 	100,000	-> vector(int:400KB, long:800KB)

const int MAX = 200000;

void ComInit(vector<long> &fac, vector<long> &finv, vector<long> &inv) {
  fac.at(0) = 1;
  fac.at(1) = 1;
  finv.at(0) = 1;
  finv.at(1) = 1;
  inv.at(1) = 1;
  for (int i = 2; i < MAX; i++) {
    fac.at(i) = (fac.at(i - 1) * i) % MOD;
    inv.at(i) = MOD - (inv.at(MOD % i) * (MOD / i)) % MOD;
    finv.at(i) = (finv.at(i - 1) * inv.at(i)) % MOD;
  }
}

long COM(int N, int K, vector<long> &fac, vector<long> &finv) {
  if (N < K)
    return 0;
  if ((N < 0) || (K < 0))
    return 0;
  return (fac.at(N) * ((finv.at(K) * finv.at(N - K)) % MOD)) % MOD;
}

int main() {
  // Array
  vector<long> fac(MAX);
  vector<long> finv(MAX);
  vector<long> inv(MAX);
  // Initialize
  ComInit(fac, finv, inv);
  /*
  // Example
  cout << "N?";
  int i;
  cin >>i;
  for (int j=0; j<=i; j++)
          cout << i << 'C' << j << " = " << COM(i, j, fac, finv) << endl;
  */
  long n;
  cin >> n;
  vector<long> a(n + 2, 0);
  vector<long> b(n + 1, 0);
  long l = 0;
  long r = 0;
  for (int i = 1; i <= n + 1; i++) {
    cin >> a.at(i);
    if (0 == b.at(a.at(i)))
      b.at(a.at(i)) = i;
    else {
      l = b.at(a.at(i));
      r = i;
    }
  }
  for (int long i = 1; i <= n + 1; i++) {
    long S = COM(n + 1, i, fac, finv);
    S += MOD - COM(n - r + l, i - 1, fac, finv);
    S %= MOD;
    cout << S << endl;
  }
  return 0;
}