Problem p02990 (C++)

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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

#define ll long long
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define pu push
#define pp pop_back
#define in insert
#define ld long double
#define forn(low, high, i) for (i = low; i < high; i++)
#define forrev(high, low, i) for (i = high; i >= low; i--)
#define trace(x) cerr << #x << ": " << x << " " << endl;
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define line cout << __LINE__;
#define prv(a)                                                                 \
  for (auto x : a)                                                             \
    cout << x << ' ';                                                          \
  cout << '\n';
#define decimal_digits cout << fixed << setprecision(15);
#define dbg2(a, b) cerr << #a << " = " << a << " " << #b << " = " << b << '\n';
#define debug(x) cerr << __LINE__ << ' ' << #x << " = " << x << '\n';
#define dln cerr << '\n';
#define dsp cerr << ' ';
#define pln cout << '\n';
#define psp cout << ' ';

typedef unordered_map<int, int> umi;
typedef unordered_map<ll, ll> uml;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

clock_t time_p = clock();

void ktj() {
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}

const ll mod = 1e9 + 7;

const ll N = 2000 + 5;

ll f[N];

inline ll add(ll a, ll b) { return (a % mod + b % mod) % mod; }

inline ll sub(ll a, ll b) { return (a % mod - b % mod + mod) % mod; }

inline ll mul(ll a, ll b) { return (a % mod * b % mod) % mod; }

inline ll modexpo(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res % mod * a % mod) % mod;
    a = (a % mod * a % mod) % mod;
    b >>= 1;
  }
  return res;
}

inline ll modinv_Fermat(ll a) { return modexpo(a, mod - 2); }

inline ll divide(ll a, ll b) {
  return (a % mod * modexpo(b, mod - 2) % mod) % mod;
}

int main() {
  fastio;
  ll n, k;
  ll i, j, ans = 0;
  cin >> n >> k;
  f[0] = 1;
  forn(1, N, i) { f[i] = mul(i, f[i - 1]); }

  forn(1, k + 1, i) {
    ll blue = f[k - 1];
    blue = divide(blue, f[i - 1]);
    blue = divide(blue, f[k - i]);
    ll red = f[n - k + 1];
    red = divide(red, f[i]);
    red = divide(red, f[n - k + 1 - i]);
    ans = mul(red, blue);
    cout << ans << '\n';
  }

  ktj();
}


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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

#define ll long long
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define pu push
#define pp pop_back
#define in insert
#define ld long double
#define forn(low, high, i) for (i = low; i < high; i++)
#define forrev(high, low, i) for (i = high; i >= low; i--)
#define trace(x) cerr << #x << ": " << x << " " << endl;
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define line cout << __LINE__;
#define prv(a)                                                                 \
  for (auto x : a)                                                             \
    cout << x << ' ';                                                          \
  cout << '\n';
#define decimal_digits cout << fixed << setprecision(15);
#define dbg2(a, b) cerr << #a << " = " << a << " " << #b << " = " << b << '\n';
#define debug(x) cerr << __LINE__ << ' ' << #x << " = " << x << '\n';
#define dln cerr << '\n';
#define dsp cerr << ' ';
#define pln cout << '\n';
#define psp cout << ' ';

typedef unordered_map<int, int> umi;
typedef unordered_map<ll, ll> uml;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

clock_t time_p = clock();

void ktj() {
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}

const ll mod = 1e9 + 7;

const ll N = 2000 + 5;

ll f[N];

inline ll add(ll a, ll b) { return (a % mod + b % mod) % mod; }

inline ll sub(ll a, ll b) { return (a % mod - b % mod + mod) % mod; }

inline ll mul(ll a, ll b) { return (a % mod * b % mod) % mod; }

inline ll modexpo(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res % mod * a % mod) % mod;
    a = (a % mod * a % mod) % mod;
    b >>= 1;
  }
  return res;
}

inline ll modinv_Fermat(ll a) { return modexpo(a, mod - 2); }

inline ll divide(ll a, ll b) {
  return (a % mod * modexpo(b, mod - 2) % mod) % mod;
}

int main() {
  fastio;
  ll n, k;
  ll i, j, ans = 0;
  cin >> n >> k;
  f[0] = 1;
  forn(1, N, i) { f[i] = mul(i, f[i - 1]); }

  forn(1, k + 1, i) {
    ll blue = f[k - 1];
    blue = divide(blue, f[i - 1]);
    blue = divide(blue, f[k - i]);
    ll red;
    if (n - k + 1 >= i) {
      red = f[n - k + 1];
      red = divide(red, f[i]);
      red = divide(red, f[n - k + 1 - i]);
    } else {
      red = 0;
    }
    ans = mul(red, blue);
    cout << ans << '\n';
  }

  ktj();
}