Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 2e9;
const int MOD = 1e9 + 7;

struct mint {
  ll x;
  mint(ll x = 0) { (this->x = (x + MOD)) %= MOD; }
  mint &operator+=(const mint a) {
    if ((this->x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((this->x += MOD - a.x) >= MOD)
      this->x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (this->x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

mint c[4005][4005];
void init() {
  c[0][0] = 1;
  rep(i, 0, 4000) {
    rep(j, 0, i + 1) {
      c[i + 1][j] += c[i][j];
      c[i + 1][j + 1] += c[i][j];
    }
  }
}
mint comb(int n, int k) { return c[n][k]; }
mint f2(int n, int k) { return comb(n + k - 1, k - 1); }
mint f(int n, int k) {
  if (n < k)
    return 0;
  return f2(n - k, k);
}
int main() {
  init();
  int n, k;
  cin >> n >> k;
  rep(i, 1, k + 1) {
    mint blue = f(k, i);
    mint red = 0;
    red += f(n - k, i - 1);
    red += f(n - k, i);
    red += f(n - k, i);
    red += f(n - k, i + 1);
    mint ans = blue * red;
    printf("%lld\n", ans.x);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 2e9;
const int MOD = 1e9 + 7;

struct mint {
  ll x;
  mint(ll x = 0) { (this->x = (x + MOD)) %= MOD; }
  mint &operator+=(const mint a) {
    if ((this->x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((this->x += MOD - a.x) >= MOD)
      this->x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (this->x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

mint c[4005][4005];
void init() {
  c[0][0] = 1;
  rep(i, 0, 4000) {
    rep(j, 0, i + 1) {
      c[i + 1][j] += c[i][j];
      c[i + 1][j + 1] += c[i][j];
    }
  }
}
mint comb(int n, int k) { return c[n][k]; }
mint f2(int n, int k) { return comb(n + k - 1, k - 1); }
mint f(int n, int k) {
  if (n < k)
    return 0;
  if (n == 0 && k == 0)
    return 1;
  if (k < 1)
    return 0;
  return f2(n - k, k);
}
int main() {
  init();
  int n, k;
  cin >> n >> k;
  rep(i, 1, k + 1) {
    mint blue = f(k, i);
    mint red = 0;
    red += f(n - k, i - 1);
    red += f(n - k, i);
    red += f(n - k, i);
    red += f(n - k, i + 1);
    mint ans = blue * red;
    printf("%lld\n", ans.x);
  }
  return 0;
}