Problem p03674 (C++)

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

using ll = long long;
using namespace std;

// gcd(a,b) および ax+by=gcd(a,b) となる x, y の導出
ll Extgcd(ll a, ll b, ll &x, ll &y) {
  ll g = a;
  if (b != 0) {
    Extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1, y = 0;
  }
  return g;
}

// ax = gcd(a, m)となるxの導出
// a,m が互いにその時、x は mod m での a のモジュラ逆数となる
ll ModInverse(ll a, ll m) {
  ll x, y;
  Extgcd(a, m, x, y);
  return (x % m + m) % m;
}

// mod MOD での i! のモジュラ逆数テーブルの生成
// i は 0 ~ MAX_NUM-1
constexpr ll MOD = 1000000007;
constexpr ll MAX_NUM = 10000 * 2;

vector<ll> fact(MAX_NUM);
vector<ll> rfact(MAX_NUM);

void FactInit() {
  fact[0] = rfact[0] = 1;
  for (int i = 1; i < MAX_NUM; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    rfact[i] = rfact[i - 1] * ModInverse(i, MOD) % MOD;
  }
}

// C(n,k) (mod MOD)の導出
inline ll Combination(ll n, ll k) {
  return (fact[n] * rfact[k] % MOD) * rfact[n - k] % MOD;
}

int main() {
  FactInit();
  int num;
  vector<int> v;
  cin >> num;
  for (int i = 0; i < num + 1; i++) {
    int input;
    cin >> input;
    v.push_back(input);
  }

  int dup;
  vector<int> v2;
  copy(v.begin(), v.end(), back_inserter(v2));
  sort(v2.begin(), v2.end());
  for (int i = 0; i < num; i++) {
    if (v2[i] == v2[i + 1]) {
      dup = v2[i];
    }
  }
  int dup_id1, dup_id2;
  for (int i = 0; i < num + 1; i++) {
    if (v[i] == dup) {
      dup_id1 = i;
      for (int j = i + 1; j < num + 1; j++) {
        if (v[j] == dup) {
          dup_id2 = j;
          break;
        }
      }
      break;
    }
  }

  for (int i = 1; i <= num + 1; i++) {
    cout << ((Combination(num + 1, i) -
              (dup_id1 + num - dup_id2 >= i - 1
                   ? Combination(dup_id1 + num - dup_id2, i - 1)
                   : 0)) %
                 MOD +
             MOD) %
                MOD
         << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using ll = long long;
using namespace std;

// gcd(a,b) および ax+by=gcd(a,b) となる x, y の導出
ll Extgcd(ll a, ll b, ll &x, ll &y) {
  ll g = a;
  if (b != 0) {
    Extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1, y = 0;
  }
  return g;
}

// ax = gcd(a, m)となるxの導出
// a,m が互いにその時、x は mod m での a のモジュラ逆数となる
ll ModInverse(ll a, ll m) {
  ll x, y;
  Extgcd(a, m, x, y);
  return (x % m + m) % m;
}

// mod MOD での i! のモジュラ逆数テーブルの生成
// i は 0 ~ MAX_NUM-1
constexpr ll MOD = 1000000007;
constexpr ll MAX_NUM = 100000 * 2;

vector<ll> fact(MAX_NUM);
vector<ll> rfact(MAX_NUM);

void FactInit() {
  fact[0] = rfact[0] = 1;
  for (int i = 1; i < MAX_NUM; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    rfact[i] = rfact[i - 1] * ModInverse(i, MOD) % MOD;
  }
}

// C(n,k) (mod MOD)の導出
inline ll Combination(ll n, ll k) {
  return (fact[n] * rfact[k] % MOD) * rfact[n - k] % MOD;
}

int main() {
  FactInit();
  int num;
  vector<int> v;
  cin >> num;
  for (int i = 0; i < num + 1; i++) {
    int input;
    cin >> input;
    v.push_back(input);
  }

  int dup;
  vector<int> v2;
  copy(v.begin(), v.end(), back_inserter(v2));
  sort(v2.begin(), v2.end());
  for (int i = 0; i < num; i++) {
    if (v2[i] == v2[i + 1]) {
      dup = v2[i];
    }
  }
  int dup_id1, dup_id2;
  for (int i = 0; i < num + 1; i++) {
    if (v[i] == dup) {
      dup_id1 = i;
      for (int j = i + 1; j < num + 1; j++) {
        if (v[j] == dup) {
          dup_id2 = j;
          break;
        }
      }
      break;
    }
  }

  for (int i = 1; i <= num + 1; i++) {
    cout << ((Combination(num + 1, i) -
              (dup_id1 + num - dup_id2 >= i - 1
                   ? Combination(dup_id1 + num - dup_id2, i - 1)
                   : 0)) %
                 MOD +
             MOD) %
                MOD
         << endl;
  }

  return 0;
}