Problem p02769 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // minmax, sort, swap
#include <cmath>
#include <cstdio>     // printf, scanf
#include <deque>      // deque
#include <functional> // function<void(int)>
#include <iomanip>    // cout<<setprecision(n)
#include <iostream>   // cin, cout, cerr
#include <map>        // key-value pairs sorted by keys
#include <numeric>    // iota
#include <queue>      // queue, priority_queue
#include <set>        // set
#include <string>     // string, stoi, to_string
#include <vector>     // vector

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define int long long // at least int64 > 9*10^18
#define ENDL '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */

constexpr int MOD = 1e9 + 7;
struct mint {
  int v;
  mint() : v(0) {}
  mint(int v) : v((v + MOD) % MOD) {}
  mint operator-() const { return mint(0) - *this; }
  mint &operator+=(const mint &a) {
    if ((v += a.v) >= MOD)
      v -= MOD;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((v += MOD - a.v) >= MOD)
      v -= MOD;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (v *= a.v) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a) {
    (*this) *= a.inv();
    return *this;
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return v < a.v; }
  bool operator==(const mint &a) const { return v == a.v; }
  mint pow(int k) const {
    mint r(1), t(v);
    while (k) {
      if (k & 1)
        r *= t;
      t *= t;
      k >>= 1;
    }
    return r;
  }
  mint inv() const { return pow(MOD - 2); }
  static mint comb(int n, int k) {
    if (n - k < k)
      k = n - k;
    mint num(1), dom(1);
    for (int i = 0; i < k; i++) {
      num *= n - i;
      dom *= i + 1;
    }
    return num / dom;
  }
};
std::istream &operator>>(std::istream &i, mint &a) {
  int t;
  i >> t;
  a = mint(t);
  return i;
}
std::ostream &operator<<(std::ostream &o, const mint &a) {
  o << a.v;
  return o;
}

std::vector<mint> construct_comb(int n) {
  std::vector<mint> c(n + 1);
  mint a = 1;
  c[0] = a;
  for (int i = 1; i <= n; i++) {
    a = a * mint(n + 1 - i) / i;
    c[i] = a;
  }
  return c;
}

signed main() {
  int n, k;
  std::cin >> n >> k;
  // sum of nCk*(n-1)Ck
  auto n0 = construct_comb(n);
  auto n1 = construct_comb(n - 1);
  mint sum = 0;
  for (int i = 0; i <= k; i++) {
    sum += n0[i] * n1[i];
  }
  print(sum);

  return 0;
}


Fixed submission:
#include <algorithm> // minmax, sort, swap
#include <cmath>
#include <cstdio>     // printf, scanf
#include <deque>      // deque
#include <functional> // function<void(int)>
#include <iomanip>    // cout<<setprecision(n)
#include <iostream>   // cin, cout, cerr
#include <map>        // key-value pairs sorted by keys
#include <numeric>    // iota
#include <queue>      // queue, priority_queue
#include <set>        // set
#include <string>     // string, stoi, to_string
#include <vector>     // vector

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define int long long // at least int64 > 9*10^18
#define ENDL '\n'
#define rep(i, n) for (int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */

constexpr int MOD = 1e9 + 7;
struct mint {
  int v;
  mint() : v(0) {}
  mint(int v) : v((v + MOD) % MOD) {}
  mint operator-() const { return mint(0) - *this; }
  mint &operator+=(const mint &a) {
    if ((v += a.v) >= MOD)
      v -= MOD;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((v += MOD - a.v) >= MOD)
      v -= MOD;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (v *= a.v) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a) {
    (*this) *= a.inv();
    return *this;
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return v < a.v; }
  bool operator==(const mint &a) const { return v == a.v; }
  mint pow(int k) const {
    mint r(1), t(v);
    while (k) {
      if (k & 1)
        r *= t;
      t *= t;
      k >>= 1;
    }
    return r;
  }
  mint inv() const { return pow(MOD - 2); }
  static mint comb(int n, int k) {
    if (n - k < k)
      k = n - k;
    mint num(1), dom(1);
    for (int i = 0; i < k; i++) {
      num *= n - i;
      dom *= i + 1;
    }
    return num / dom;
  }
};
std::istream &operator>>(std::istream &i, mint &a) {
  int t;
  i >> t;
  a = mint(t);
  return i;
}
std::ostream &operator<<(std::ostream &o, const mint &a) {
  o << a.v;
  return o;
}

std::vector<mint> construct_comb(int n) {
  std::vector<mint> c(n + 1);
  mint a = 1;
  c[0] = a;
  for (int i = 1; i <= n; i++) {
    a = a * mint(n + 1 - i) / i;
    c[i] = a;
  }
  return c;
}

signed main() {
  int n, k;
  std::cin >> n >> k;
  // sum of nCk*(n-1)Ck
  auto n0 = construct_comb(n);
  auto n1 = construct_comb(n - 1);
  k = std::min(k, n - 1);
  mint sum = 0;
  for (int i = 0; i <= k; i++) {
    sum += n0[i] * n1[i];
  }
  print(sum);

  return 0;
}