Problem p03676 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) < (b))
#define DUPLE(a, b, c, d)                                                      \
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pll Dir[8] = { // 移動
    {0, 1}, {-1, 0}, {1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

//[a, b)
#define Getsum(ar, a, b) (ar[b] - ar[a])
#define INF 10000000000000000LL

struct Edge {
  int from, to;
  ll w;
};
typedef vector<vector<Edge>> Graph;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll m, ll n) { return m * n / gcd(m, n); }

int mod_pow(int x, int n, int mod = MODU) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = (ll)x * x % mod;
    n >>= 1;
  }
  return res;
}
int divmod(int a, int b, int m = MODU) { // a/b(mod m)
                                         // a/b (mod m)
                                         // a*b^-1
                                         // b^-1 * b = 1
                                         // b^(m-1) = 1
                                         // b^(m-2) * b = 1
                                         // b^-1 = b^(m-2)

  // a/b = a * b^(m-2)

  return (a * 1LL * mod_pow(b, m - 2, m)) % m;
}

int fact[100001];

int factorial(int x) {
  if (x == 0)
    return fact[x] = 1;

  fact[x] = ((ll)factorial(x - 1) * x) % MODU;
  return fact[x];
}

int Comb(int n, int r) {
  return divmod(fact[n], (ll)fact[n - r] * fact[r] % MODU);
}

signed main(void) {
  int n;
  cin >> n;
  n++;
  vector<int> a(n);

  factorial(n);
  map<int, int> pos;
  int l, r;
  REP(i, n) {
    cin >> a[i];
    if (pos.count(a[i])) {
      l = pos[a[i]];
      r = i;
    } else {
      pos[a[i]] = i;
    }
  }

  rep(i, 1, n + 1) {
    int aa, bb;
    printf("%d\n",
           ((aa = Comb(n, i)) - (bb = Comb(l + n - r - 1, i - 1)) + MODU) %
               MODU);
    // cout << "";
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) < (b))
#define DUPLE(a, b, c, d)                                                      \
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pll Dir[8] = { // 移動
    {0, 1}, {-1, 0}, {1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

//[a, b)
#define Getsum(ar, a, b) (ar[b] - ar[a])
#define INF 10000000000000000LL

struct Edge {
  int from, to;
  ll w;
};
typedef vector<vector<Edge>> Graph;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll m, ll n) { return m * n / gcd(m, n); }

int mod_pow(int x, int n, int mod = MODU) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = (ll)x * x % mod;
    n >>= 1;
  }
  return res;
}
int divmod(int a, int b, int m = MODU) { // a/b(mod m)
                                         // a/b (mod m)
                                         // a*b^-1
                                         // b^-1 * b = 1
                                         // b^(m-1) = 1
                                         // b^(m-2) * b = 1
                                         // b^-1 = b^(m-2)

  // a/b = a * b^(m-2)

  return (a * 1LL * mod_pow(b, m - 2, m)) % m;
}

int fact[100001];

int factorial(int x) {
  if (x == 0)
    return fact[x] = 1;

  fact[x] = ((ll)factorial(x - 1) * x) % MODU;
  return fact[x];
}

int Comb(int n, int r) {
  if (n < r)
    return 0;
  return divmod(fact[n], (ll)fact[n - r] * fact[r] % MODU);
}

signed main(void) {
  int n;
  cin >> n;
  n++;
  vector<int> a(n);

  factorial(n);
  map<int, int> pos;
  int l, r;
  REP(i, n) {
    cin >> a[i];
    if (pos.count(a[i])) {
      l = pos[a[i]];
      r = i;
    } else {
      pos[a[i]] = i;
    }
  }

  rep(i, 1, n + 1) {
    int aa, bb;
    printf("%d\n",
           ((aa = Comb(n, i)) - (bb = Comb(l + n - r - 1, i - 1)) + MODU) %
               MODU);
    // cout << "";
  }

  return 0;
}