Problem p02990 (C++)

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()

using namespace std;
using namespace __gnu_pbds;

template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;

using point = complex<double>;

void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

const double EPS = 1e-8;
const long long mod = 1e9 + 7;

const int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, -1, 1};

const int N = 5e3 + 5;
const long long INF = 1e18;

int add(int a, int b) { return (a + b) % mod; }
int mul(int a, int b) { return (1ll * a * b) % mod; }
int fp(int b, int p) {
  int ret = 1;
  while (p) {
    if (p & 1)
      ret = mul(ret, b);
    b = mul(b, b);
    p >>= 1;
  }
  return ret;
}

int f[N];
int inv[N];
void init() {
  f[0] = 1;
  inv[0] = 1;
  for (int i = 1; i < N; ++i) {
    f[i] = mul(i, f[i - 1]);
    inv[i] = fp(f[i], mod - 2);
  }
}
int nCr(int n, int r) { return mul(f[n], mul(inv[n - r], inv[r])); }
/*
int dp[2005][2005][4];
int solve(int blue, int red,int lstBlue){

        if(blue < 0 || red < 0)
                return 0;
        if(blue == 0 and red == 0)
                return 1;
        int &ret = dp[blue][red][lstBlue];
        if(~ret)
            return ret;
        ret = 0;
        if(lstBlue == 2){ // didn't put any ball yet
            ret = add(ret, solve(blue - 1,red,1));
            ret = add(ret, solve(blue,red - 1,0));
        }else if(lstBlue == 1){ // last ball was blue
            ret = add(ret,solve(blue,red - 1,0));
        }else{ // last ball was red
            ret = add(ret, solve(blue , red - 1,0));
            ret = add(ret, solve(blue - 1,red,1));
        }
        return ret;
}*/
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  init();
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; ++i) {
    cout << mul(nCr(n - k + 1, i), nCr(k - 1, i - 1)) << '\n';
  }
  return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define error(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()

using namespace std;
using namespace __gnu_pbds;

template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;

using point = complex<double>;

void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

const double EPS = 1e-8;
const long long mod = 1e9 + 7;

const int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, -1, 1};

const int N = 5e3 + 5;
const long long INF = 1e18;

int add(int a, int b) { return (a + b) % mod; }
int mul(int a, int b) { return (1ll * a * b) % mod; }
int fp(int b, int p) {
  int ret = 1;
  while (p) {
    if (p & 1)
      ret = mul(ret, b);
    b = mul(b, b);
    p >>= 1;
  }
  return ret;
}

int f[N];
int inv[N];
void init() {
  f[0] = 1;
  inv[0] = 1;
  for (int i = 1; i < N; ++i) {
    f[i] = mul(i, f[i - 1]);
    inv[i] = fp(f[i], mod - 2);
  }
}
int nCr(int n, int r) { return mul(f[n], mul(inv[n - r], inv[r])); }
/*
int dp[2005][2005][4];
int solve(int blue, int red,int lstBlue){

        if(blue < 0 || red < 0)
                return 0;
        if(blue == 0 and red == 0)
                return 1;
        int &ret = dp[blue][red][lstBlue];
        if(~ret)
            return ret;
        ret = 0;
        if(lstBlue == 2){ // didn't put any ball yet
            ret = add(ret, solve(blue - 1,red,1));
            ret = add(ret, solve(blue,red - 1,0));
        }else if(lstBlue == 1){ // last ball was blue
            ret = add(ret,solve(blue,red - 1,0));
        }else{ // last ball was red
            ret = add(ret, solve(blue , red - 1,0));
            ret = add(ret, solve(blue - 1,red,1));
        }
        return ret;
}*/
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  init();
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; ++i) {
    if (n - k + 1 >= i and k >= i)
      cout << mul(nCr(n - k + 1, i), nCr(k - 1, i - 1)) << '\n';
    else
      cout << 0 << '\n';
  }
  return 0;
}