Problem p02990 (C++)

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

using namespace std;

using ll = long long;
using ld = long double;

const double PI = 3.1415926535897932384626433832795;
// const ll MOD = 1000000007;

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

int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : abs(x); }
int lcm(int x, int y) { return x / gcd(x, y) * y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return ModInt(u);
  }

  friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt<mod> &a) {
    long long x;
    is >> x;
    a = ModInt<mod>(x);
    return (is);
  }
};

const int mod = 1e9 + 7;
using modint = ModInt<mod>;

modint c[4000][4000];
void init() {
  c[0][0] = 1;
  for (int i = 0; i <= 4000; i++) {
    for (int j = 0; j <= i; j++) {
      c[i + 1][j] += c[i][j];
      c[i + 1][j + 1] += c[i][j];
    }
  }
}

modint comb(int n, int k) { return c[n][k]; }

modint f2(int n, int k) { return comb(n + k - 1, k - 1); }
modint 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() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);

  init();
  int n, k;
  cin >> n >> k;

  for (int i = 1; i <= k; i++) {
    modint blue = f(k, i);
    modint red = 0;
    red += f(n - k, i - 1);
    red += f(n - k, i);
    red += f(n - k, i);
    red += f(n - k, i + 1);
    modint ans = blue * red;
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

const double PI = 3.1415926535897932384626433832795;
// const ll MOD = 1000000007;

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

int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : abs(x); }
int lcm(int x, int y) { return x / gcd(x, y) * y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return ModInt(u);
  }

  friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt<mod> &a) {
    long long x;
    is >> x;
    a = ModInt<mod>(x);
    return (is);
  }
};

const int mod = 1e9 + 7;
using modint = ModInt<mod>;

modint c[4030][4030];
void init() {
  c[0][0] = 1;
  for (int i = 0; i <= 4000; i++) {
    for (int j = 0; j <= i; j++) {
      c[i + 1][j] += c[i][j];
      c[i + 1][j + 1] += c[i][j];
    }
  }
}

modint comb(int n, int k) { return c[n][k]; }

modint f2(int n, int k) { return comb(n + k - 1, k - 1); }
modint 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() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);

  init();
  int n, k;
  cin >> n >> k;

  for (int i = 1; i <= k; i++) {
    modint blue = f(k, i);
    modint red = 0;
    red += f(n - k, i - 1);
    red += f(n - k, i);
    red += f(n - k, i);
    red += f(n - k, i + 1);
    modint ans = blue * red;
    cout << ans << endl;
  }
  return 0;
}