Problem p02990 (C++)

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

#include <cmath>
#include <cstdio>
#include <cstdlib>

#include <iostream>

#include <algorithm>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

const ll mod = 1000000007;

class mint {
public:
  ll x;
  mint(ll x = 0) : x(x % mod) {}
  mint &operator+=(const mint rhs) {
    x += rhs.x;
    while (x >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint rhs) {
    x -= rhs.x;
    while (x < 0)
      x += mod;
    return *this;
  }
  mint &operator*=(const mint rhs) {
    (x *= rhs.x) %= mod;
    return *this;
  }
  mint operator+(const mint rhs) const {
    mint res(*this);
    return res += rhs;
  }
  mint operator-(const mint rhs) const {
    mint res(*this);
    return res -= rhs;
  }
  mint operator*(const mint rhs) const {
    mint res(*this);
    return res *= rhs;
  }
  bool operator<(const ll rhs) const { return x < rhs; }
  bool operator>(const ll rhs) const { return x > rhs; }
  friend ostream &operator<<(ostream &os, const mint rhs) {
    os << rhs.x;
    return os;
  }
};

#define rep(index, num) for (int index = 0; index < num; ++index)

mint memo[2005][2005];
mint comb(int n, int m) {
  if (memo[n][m] > 0)
    return memo[n][m];
  if (m == 0 || m == n) {
    memo[n][m] = 1;
    return 1;
  } else {
    memo[n][m] = comb(n - 1, m) + comb(n - 1, m - 1);
    return memo[n][m];
  }
}

int main() {
  int N, K;
  std::cin >> N >> K;
  rep(i, 2005) rep(j, 2005) memo[i][j] = 0;
  for (int i = 1; i <= K; i++) {
    cout << comb(N - K + 1, i) * comb(K - 1, i - 1) << endl;
  }
  return 0;
}


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

#include <cmath>
#include <cstdio>
#include <cstdlib>

#include <iostream>

#include <algorithm>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

const ll mod = 1000000007;

class mint {
public:
  ll x;
  mint(ll x = 0) : x(x % mod) {}
  mint &operator+=(const mint rhs) {
    x += rhs.x;
    while (x >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint rhs) {
    x -= rhs.x;
    while (x < 0)
      x += mod;
    return *this;
  }
  mint &operator*=(const mint rhs) {
    (x *= rhs.x) %= mod;
    return *this;
  }
  mint operator+(const mint rhs) const {
    mint res(*this);
    return res += rhs;
  }
  mint operator-(const mint rhs) const {
    mint res(*this);
    return res -= rhs;
  }
  mint operator*(const mint rhs) const {
    mint res(*this);
    return res *= rhs;
  }
  bool operator<(const ll rhs) const { return x < rhs; }
  bool operator>(const ll rhs) const { return x > rhs; }
  friend ostream &operator<<(ostream &os, const mint rhs) {
    os << rhs.x;
    return os;
  }
};

#define rep(index, num) for (int index = 0; index < num; ++index)

mint memo[2005][2005];
mint comb(int n, int m) {
  if (m > n)
    return 0;
  if (memo[n][m] > 0)
    return memo[n][m];
  if (m == 0 || m == n) {
    memo[n][m] = 1;
    return 1;
  } else {
    memo[n][m] = comb(n - 1, m) + comb(n - 1, m - 1);
    return memo[n][m];
  }
}

int main() {
  int N, K;
  std::cin >> N >> K;
  rep(i, 2005) rep(j, 2005) memo[i][j] = 0;
  for (int i = 1; i <= K; i++) {
    cout << comb(N - K + 1, i) * comb(K - 1, i - 1) << endl;
  }
  return 0;
}