Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define LL long long
#define pii pair<int, int>
#define piii pair<int, pii>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define vi vector<int>
#define vpi vector<pii>
#define INF 2147483647
#define big 20160116
#define pb push_back
#define mp make_pair
using namespace std;
const int Maxk = 1005;
string mul(string a, string b) {
  int res[Maxk];
  memset(res, 0, sizeof(res));
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  for (int i = 0; i < a.length(); i++) {
    for (int j = 0; j < b.length(); j++) {
      res[i + j] += (a[i] - '0') * (b[j] - '0');
    }
  }
  for (int i = 0; i < Maxk; i++) {
    if (res[i] >= 10) {
      res[i + 1] += (res[i] / 10);
      res[i] %= 10;
    }
  }
  string ret;
  bool flag = false;
  for (int i = Maxk - 1; i >= 0; i--) {
    if (flag || res[i]) {
      flag = true;
      ret = ret + (char)(res[i] + '0');
    }
  }
  if (ret == "")
    ret = "0";
  return ret;
}
string add(string a, string b) {
  if (a.length() < b.length())
    swap(a, b);
  while (a.length() != b.length()) {
    b = '0' + b;
  }
  for (int i = a.length() - 1; i >= 0; i--) {
    a[i] += (b[i] - '0');
    if (a[i] > '9' && i) {
      a[i] -= 10;
      a[i - 1]++;
    }
  }
  if (a[0] > '9') {
    a[0] -= 10;
    a = '1' + a;
  }
  return a;
}
const int Maxn = 100005;
const long long mod = 1000000007;
long long fact[Maxn];
long long qp(long long x, long long p) {
  if (p == 0)
    return 1LL;
  if (p == 1)
    return x;
  if (p % 2 == 0LL) {
    long long y = qp(x, p / 2);
    return y * y % mod;
  } else {
    long long y = qp(x, p / 2);
    return y * y % mod * x % mod;
  }
}
long long inv(long long x) { return qp(x, mod - 2); }
long long c(long long n, long long k) {
  if (k > n)
    return 0;
  if (k < 0)
    return 0;
  if (k * 2 > n)
    k = n - k;
  long long x = fact[n];
  x = x * inv(fact[n - k]);
  x %= mod;
  x *= inv(fact[k]);
  x %= mod;
  return x;
}
int a[Maxn];
int use[Maxn];
int main() {
  fact[0] = 1;
  for (int i = 1; i < Maxn; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= mod;
  }
  int n;
  scanf("%d", &n);
  int dist = 0;
  memset(use, 0, sizeof(use));
  for (int i = 0; i < n + 1; i++) {
    scanf("%d", &a[i]);
    if (use[a[i]] != 0) {
      dist = i - use[a[i]];
    }
    use[a[i]] = i + 1;
  }
  dist = n - 1 - dist;
  //	cout<<dist<<endl;
  for (int i = 1; i <= n + 1; i++) {
    long long res = c(n + 1, i);
    res -= c(dist, i - 1);
    if (res < 0)
      throw 3;
    printf("%I64d\n", res);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define LL long long
#define pii pair<int, int>
#define piii pair<int, pii>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define vi vector<int>
#define vpi vector<pii>
#define INF 2147483647
#define big 20160116
#define pb push_back
#define mp make_pair
using namespace std;
const int Maxk = 1005;
string mul(string a, string b) {
  int res[Maxk];
  memset(res, 0, sizeof(res));
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  for (int i = 0; i < a.length(); i++) {
    for (int j = 0; j < b.length(); j++) {
      res[i + j] += (a[i] - '0') * (b[j] - '0');
    }
  }
  for (int i = 0; i < Maxk; i++) {
    if (res[i] >= 10) {
      res[i + 1] += (res[i] / 10);
      res[i] %= 10;
    }
  }
  string ret;
  bool flag = false;
  for (int i = Maxk - 1; i >= 0; i--) {
    if (flag || res[i]) {
      flag = true;
      ret = ret + (char)(res[i] + '0');
    }
  }
  if (ret == "")
    ret = "0";
  return ret;
}
string add(string a, string b) {
  if (a.length() < b.length())
    swap(a, b);
  while (a.length() != b.length()) {
    b = '0' + b;
  }
  for (int i = a.length() - 1; i >= 0; i--) {
    a[i] += (b[i] - '0');
    if (a[i] > '9' && i) {
      a[i] -= 10;
      a[i - 1]++;
    }
  }
  if (a[0] > '9') {
    a[0] -= 10;
    a = '1' + a;
  }
  return a;
}
const int Maxn = 100005;
const long long mod = 1000000007;
long long fact[Maxn];
long long qp(long long x, long long p) {
  if (p == 0)
    return 1LL;
  if (p == 1)
    return x;
  if (p % 2 == 0LL) {
    long long y = qp(x, p / 2);
    return y * y % mod;
  } else {
    long long y = qp(x, p / 2);
    return y * y % mod * x % mod;
  }
}
long long inv(long long x) { return qp(x, mod - 2); }
long long c(long long n, long long k) {
  if (k > n)
    return 0;
  if (k < 0)
    return 0;
  if (k * 2 > n)
    k = n - k;
  long long x = fact[n];
  x = x * inv(fact[n - k]);
  x %= mod;
  x *= inv(fact[k]);
  x %= mod;
  return x;
}
int a[Maxn];
int use[Maxn];
int main() {
  fact[0] = 1;
  for (int i = 1; i < Maxn; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= mod;
  }
  int n;
  scanf("%d", &n);
  int dist = 0;
  memset(use, 0, sizeof(use));
  for (int i = 0; i < n + 1; i++) {
    scanf("%d", &a[i]);
    if (use[a[i]] != 0) {
      dist = i - use[a[i]];
    }
    use[a[i]] = i + 1;
  }
  dist = n - 1 - dist;
  //	cout<<dist<<endl;
  for (int i = 1; i <= n + 1; i++) {
    long long res = c(n + 1, i);
    res -= c(dist, i - 1);
    res %= mod;
    res += mod;
    res %= mod;
    if (res < 0)
      throw 3;
    printf("%I64d\n", res);
  }
  return 0;
}