Problem p03676 (C++)

Buggy submission (status: Runtime Error):
// D.

#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

typedef long long LL;

static const LL MOD = 1000000007;

struct modll {
  long long x;
  modll() {}
  modll(long long _x) : x(_x) {}
  operator int() const { return (int)x; }
  modll operator+(const modll &r) { return (x + r.x) % MOD; }
  modll operator+=(const modll &r) { return x = (x + r.x) % MOD; }
  modll operator-(const modll &r) { return (x + MOD - r.x) % MOD; }
  modll operator-=(const modll &r) { return x = (x + MOD - r.x) % MOD; }
  modll operator*(const modll &r) { return (x * r.x) % MOD; }
  modll operator*(int r) { return (x * r) % MOD; }
  modll operator*=(const modll &r) { return x = (x * r.x) % MOD; }
  static modll modinv(int a) { return modpow(a, MOD - 2); }
  static modll modpow(int a, int b) {
    modll x = a, r = 1;
    for (; b; b >>= 1, x *= x)
      if (b & 1)
        r *= x;
    return r;
  }
};

static modll fact[300003], inv[300003];

modll combination(LL n, LL r) {
  modll x = fact[n];
  x *= inv[r];
  x *= inv[n - r];
  return x;
}

int main(int argc, char *argv[]) {
  fact[0] = 1;
  inv[0] = 1;
  for (int i = 1; i <= 300002; ++i) {
    fact[i] = fact[i - 1] * i;
    inv[i] = modll::modinv(fact[i]);
  }

  int n, d;
  cin >> n;
  vector<int> pos(n + 1, -1);

  for (int i = 0; i <= n; ++i) {
    int a;
    cin >> a;
    if (pos[a] >= 0) {
      d = i - pos[a];
    }
    pos[a] = i;
  }

  for (int i = 1; i <= n + 1; ++i) {
    modll ans = combination(n + 1, i);
    ans -= combination(n - d, i - 1);
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
// D.

#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

typedef long long LL;

static const LL MOD = 1000000007;

struct modll {
  long long x;
  modll() {}
  modll(long long _x) : x(_x) {}
  operator int() const { return (int)x; }
  modll operator+(const modll &r) { return (x + r.x) % MOD; }
  modll operator+=(const modll &r) { return x = (x + r.x) % MOD; }
  modll operator-(const modll &r) { return (x + MOD - r.x) % MOD; }
  modll operator-=(const modll &r) { return x = (x + MOD - r.x) % MOD; }
  modll operator*(const modll &r) { return (x * r.x) % MOD; }
  modll operator*(int r) { return (x * r) % MOD; }
  modll operator*=(const modll &r) { return x = (x * r.x) % MOD; }
  static modll modinv(int a) { return modpow(a, MOD - 2); }
  static modll modpow(int a, int b) {
    modll x = a, r = 1;
    for (; b; b >>= 1, x *= x)
      if (b & 1)
        r *= x;
    return r;
  }
};

static modll fact[300003], inv[300003];

modll combination(LL n, LL r) {
  modll x = 0;
  if (n >= r) {
    x = fact[n];
    x *= inv[r];
    x *= inv[n - r];
  }
  return x;
}

int main(int argc, char *argv[]) {
  fact[0] = 1;
  inv[0] = 1;
  for (int i = 1; i <= 300002; ++i) {
    fact[i] = fact[i - 1] * i;
    inv[i] = modll::modinv(fact[i]);
  }

  int n, d;
  cin >> n;
  vector<int> pos(n + 1, -1);

  for (int i = 0; i <= n; ++i) {
    int a;
    cin >> a;
    if (pos[a] >= 0) {
      d = i - pos[a];
    }
    pos[a] = i;
  }

  for (int i = 1; i <= n + 1; ++i) {
    modll ans = combination(n + 1, i);
    ans -= combination(n - d, i - 1);
    cout << ans << endl;
  }
  return 0;
}