Problem p02769 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

const int mod = 1000000007;
struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    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 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 { return mint(*this) /= a; }
};

mint f(int n) {
  if (n == 0)
    return 1;
  mint x = f(n / 2);
  x *= x;
  if (n % 2 == 1)
    x *= 2;
  return x;
}

mint choose(int n, int a) {
  mint x = 1, y = 1;
  for (int i = 0; i < a; ++i) {
    x *= n - i;
    y *= i + 1;
  }
  return x / y;
}

mint fact[400400];
mint inv_fact[400400];
void init() {
  fact[0] = 1;
  for (int i = 1; i < 400400; ++i) {
    fact[i] = fact[i - 1] * i;
  }

  inv_fact[400400 - 1] = fact[400400 - 1].inv();
  for (int i = 400400 - 1; i >= 1; --i) {
    inv_fact[i - 1] = inv_fact[i] * i;
  }
}

mint comb(int n, int k) { return fact[n] * inv_fact[n - k] * inv_fact[k]; }

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

  mint ans = 1;
  if (k >= n - 1)
    ans = comb(2 * n - 1, n);
  else {
    for (int i = 1; i <= k; ++i) {
      mint tmp = 0;
      for (int j = 1; j <= min(n - i, i); ++j) {
        tmp += comb(n - i, j) * comb(i - 1, j - 1);
      }
      ans += comb(n, i) * tmp;
    }
  }

  cout << ans.x << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

const int mod = 1000000007;
struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    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 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 { return mint(*this) /= a; }
};

mint f(int n) {
  if (n == 0)
    return 1;
  mint x = f(n / 2);
  x *= x;
  if (n % 2 == 1)
    x *= 2;
  return x;
}

mint choose(int n, int a) {
  mint x = 1, y = 1;
  for (int i = 0; i < a; ++i) {
    x *= n - i;
    y *= i + 1;
  }
  return x / y;
}

mint fact[400400];
mint inv_fact[400400];
void init() {
  fact[0] = 1;
  for (int i = 1; i < 400400; ++i) {
    fact[i] = fact[i - 1] * i;
  }

  inv_fact[400400 - 1] = fact[400400 - 1].inv();
  for (int i = 400400 - 1; i >= 1; --i) {
    inv_fact[i - 1] = inv_fact[i] * i;
  }
}

mint comb(int n, int k) { return fact[n] * inv_fact[n - k] * inv_fact[k]; }

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

  mint ans = 1;
  for (int i = 1; i <= min(k, n - 1); ++i) {
    mint r = comb(n - 1, i);
    ans += comb(n, i) * r;
  }

  cout << ans.x << endl;
  return 0;
}