Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define REPE(i, n) FORE(i, 0, n)
#define FORSZ(i, a, v) FOR(i, a, SZ(v))
#define REPSZ(i, v) REP(i, SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const int MAXN = 100000;
const int MOD = 1000000007;

int n;
int a[MAXN + 1];
int cnt[MAXN];

int inv[MAXN + 2];
int fac[MAXN + 2];
int ifac[MAXN + 2];
int C(int n, int k) { return (ll)fac[n] * ifac[k] % MOD * ifac[n - k] % MOD; }

void run() {
  scanf("%d", &n);
  REP(i, n + 1) scanf("%d", &a[i]), --a[i];
  REP(i, n) cnt[i] = 0;
  REP(i, n + 1)++ cnt[a[i]];
  int dup = -1;
  REP(i, n) if (cnt[i] >= 2) dup = i;
  int l = -1, r = -1;
  REP(i, n + 1) if (a[i] == dup) {
    if (l == -1)
      l = i;
    else
      r = i;
  }
  assert(l != -1 && r != -1);

  inv[1] = 1;
  FORE(i, 2, n + 1) inv[i] = (ll)(MOD - MOD / i) * inv[MOD % i] % MOD;
  fac[0] = 1;
  FORE(i, 1, n + 1) fac[i] = (ll)fac[i - 1] * i % MOD;
  ifac[0] = 1;
  FORE(i, 1, n + 1) ifac[i] = (ll)ifac[i - 1] * inv[i] % MOD;

  FORE(i, 1, n + 1) {
    int ret = C(n + 1, i);
    int rem = n + 1 - (r - l + 1);
    int sub = C(rem, i - 1);
    if ((ret -= sub) < 0)
      ret += MOD;
    printf("%d\n", ret);
  }
}

int main() {
  run();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define REPE(i, n) FORE(i, 0, n)
#define FORSZ(i, a, v) FOR(i, a, SZ(v))
#define REPSZ(i, v) REP(i, SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const int MAXN = 100000;
const int MOD = 1000000007;

int n;
int a[MAXN + 1];
int cnt[MAXN];

int inv[MAXN + 2];
int fac[MAXN + 2];
int ifac[MAXN + 2];
int C(int n, int k) {
  if (k < 0 || k > n)
    return 0;
  return (ll)fac[n] * ifac[k] % MOD * ifac[n - k] % MOD;
}

void run() {
  scanf("%d", &n);
  REP(i, n + 1) scanf("%d", &a[i]), --a[i];
  REP(i, n) cnt[i] = 0;
  REP(i, n + 1)++ cnt[a[i]];
  int dup = -1;
  REP(i, n) if (cnt[i] >= 2) dup = i;
  int l = -1, r = -1;
  REP(i, n + 1) if (a[i] == dup) {
    if (l == -1)
      l = i;
    else
      r = i;
  }
  assert(l != -1 && r != -1);

  inv[1] = 1;
  FORE(i, 2, n + 1) inv[i] = (ll)(MOD - MOD / i) * inv[MOD % i] % MOD;
  fac[0] = 1;
  FORE(i, 1, n + 1) fac[i] = (ll)fac[i - 1] * i % MOD;
  ifac[0] = 1;
  FORE(i, 1, n + 1) ifac[i] = (ll)ifac[i - 1] * inv[i] % MOD;

  FORE(i, 1, n + 1) {
    int ret = C(n + 1, i);
    int rem = n + 1 - (r - l + 1);
    int sub = C(rem, i - 1);
    if ((ret -= sub) < 0)
      ret += MOD;
    printf("%d\n", ret);
  }
}

int main() {
  run();
  return 0;
}