Problem p02990 (C++)

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

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

typedef int64_t ll;
typedef long double ld;

const int INF = 1e9;
const ld EPS = 1e-8;

// MOD
const int64_t MOD = 1e9 + 7;
struct Fp {
  int64_t x;
  Fp() : x(0) {}
  Fp(int y) : x(y >= 0 ? y % MOD : (MOD - (-y) % MOD) % MOD) {}

  Fp &operator+=(const Fp &p) {
    x = (x + p.x) % MOD;
    return *this;
  }
  Fp &operator-=(const Fp &p) {
    x = (x + MOD - p.x) % MOD;
    return *this;
  }
  Fp &operator*=(const Fp &p) {
    x = x * p.x % MOD;
    return *this;
  }
  bool operator==(const Fp &rhs) const { return this->x == rhs.x; }
  Fp operator-() const { return Fp(-x); }
  Fp operator+(const Fp &rhs) const { return Fp(*this) += rhs; }
  Fp operator-(const Fp &rhs) const { return Fp(*this) -= rhs; }
  Fp operator*(const Fp &rhs) const { return Fp(*this) *= rhs; }
  Fp operator+(const int y) const { return Fp(*this) += Fp(y); }
  Fp operator-(const int y) const { return Fp(*this) -= Fp(y); }
  Fp operator*(const int y) const { return Fp(*this) *= Fp(y); }
};

Fp operator+(const int x, const Fp &rhs) { return Fp(x) += rhs; }
Fp operator-(const int x, const Fp &rhs) { return Fp(x) -= rhs; }
Fp operator*(const int x, const Fp &rhs) { return Fp(x) *= rhs; }

ostream &operator<<(ostream &out, const Fp &p) {
  out << p.x;
  return out;
}

int64_t inv(int64_t a, int64_t p) {
  // return x s.t. ax = 1 (mod p)
  if (a == 1)
    return 1;
  return ((1 - p * inv(p % a, a)) / a + p);
}

Fp operator/(const Fp &lhs, const Fp &rhs) { return lhs * Fp(inv(rhs.x, MOD)); }
Fp operator/(const Fp &lhs, const int x) { return (lhs / Fp(x)); }

#define MAX_FACT_N 10000

Fp fact[MAX_FACT_N], invfact[MAX_FACT_N];
void init_fact() {
  fact[0] = Fp(1);
  invfact[0] = Fp(1);
  for (int i = 1; i < MAX_FACT_N; i++) {
    fact[i] = fact[i - 1] * i;
    invfact[i] = invfact[i - 1] / i;
  }
}

Fp comb(int n, int k) { return fact[n] * invfact[k] * invfact[n - k]; }

int main() {
  int N, K;
  cin >> N >> K;
  init_fact();

  for (int i = 1; i <= K; i++) {
    Fp ret = comb(K - 1, i - 1) * comb(N - K + 1, i);
    cout << ret << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

typedef int64_t ll;
typedef long double ld;

const int INF = 1e9;
const ld EPS = 1e-8;

// MOD
const int64_t MOD = 1e9 + 7;
struct Fp {
  int64_t x;
  Fp() : x(0) {}
  Fp(int y) : x(y >= 0 ? y % MOD : (MOD - (-y) % MOD) % MOD) {}

  Fp &operator+=(const Fp &p) {
    x = (x + p.x) % MOD;
    return *this;
  }
  Fp &operator-=(const Fp &p) {
    x = (x + MOD - p.x) % MOD;
    return *this;
  }
  Fp &operator*=(const Fp &p) {
    x = x * p.x % MOD;
    return *this;
  }
  bool operator==(const Fp &rhs) const { return this->x == rhs.x; }
  Fp operator-() const { return Fp(-x); }
  Fp operator+(const Fp &rhs) const { return Fp(*this) += rhs; }
  Fp operator-(const Fp &rhs) const { return Fp(*this) -= rhs; }
  Fp operator*(const Fp &rhs) const { return Fp(*this) *= rhs; }
  Fp operator+(const int y) const { return Fp(*this) += Fp(y); }
  Fp operator-(const int y) const { return Fp(*this) -= Fp(y); }
  Fp operator*(const int y) const { return Fp(*this) *= Fp(y); }
};

Fp operator+(const int x, const Fp &rhs) { return Fp(x) += rhs; }
Fp operator-(const int x, const Fp &rhs) { return Fp(x) -= rhs; }
Fp operator*(const int x, const Fp &rhs) { return Fp(x) *= rhs; }

ostream &operator<<(ostream &out, const Fp &p) {
  out << p.x;
  return out;
}

int64_t inv(int64_t a, int64_t p) {
  // return x s.t. ax = 1 (mod p)
  if (a == 1)
    return 1;
  return ((1 - p * inv(p % a, a)) / a + p);
}

Fp operator/(const Fp &lhs, const Fp &rhs) { return lhs * Fp(inv(rhs.x, MOD)); }
Fp operator/(const Fp &lhs, const int x) { return (lhs / Fp(x)); }

#define MAX_FACT_N 10000

Fp fact[MAX_FACT_N], invfact[MAX_FACT_N];
void init_fact() {
  fact[0] = Fp(1);
  invfact[0] = Fp(1);
  for (int i = 1; i < MAX_FACT_N; i++) {
    fact[i] = fact[i - 1] * i;
    invfact[i] = invfact[i - 1] / i;
  }
}

Fp comb(int n, int k) { return fact[n] * invfact[k] * invfact[n - k]; }

int main() {
  int N, K;
  cin >> N >> K;
  init_fact();

  for (int i = 1; i <= K; i++) {
    if (N - K + 1 - i < 0) {
      cout << 0 << endl;
    } else {
      Fp ret = comb(K - 1, i - 1) * comb(N - K + 1, i);
      cout << ret << endl;
    }
  }
  return 0;
}