Problem p02769 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fup(i, a, b) for (int i = a; i <= b; ++i)
#define fdw(i, a, b) for (int i = a; i >= b; --i)
#define sync                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
#define cl(a) memset(a, 0, sizeof(a))
#define gcd __gcd
#define fi first
#define se second
#define pb push_back
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll ksm(ll a, ll b, ll mod) {
  ll ans = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
  }
  return ans;
}
// ll oula(ll n){ ll res=n;for(ll i=2;i*i<=n;i++){if(n%i==0){
// res=res-res/i;while(n%i==0) n/=i;}}if(n>1) res=res-res/n;return res;} bool
// MRT(ll x){if(x==2)return true;fup(i,1,50){ll
// now=rand()%(x-2)+2;if(ksm(now,x-1,x)!=1)return false;}return true;} void
// shai(int n)fup(i,2,n){if(isprime[i]){v.push_back(i);for(int
// j=2*i;j<=10000;j+=i)isprime[j]=0;}}//"埃氏筛筛选素数"
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-7;
const int N = 2e5 + 10;
ll fac[N], inv[N];
ll C(ll n, ll m) { return fac[n] * inv[n - m] % mod * inv[m] % mod; }
ll work(ll n, ll m) { return C(n + m - 1, m - 1); }
int main() {
  freopen("D:\\untitled3\\in", "r", stdin);
  sync;
  ll n, k;
  ll ans = 1;
  fac[0] = fac[1] = inv[0] = inv[1] = 1;
  fup(i, 2, N - 1) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = -inv[mod % i] * (mod / i) % mod;
    while (inv[i] < 0)
      inv[i] += mod;
  }
  fup(i, 2, N - 1) inv[i] = inv[i] * inv[i - 1] % mod;
  cin >> n >> k;
  k = min(k, n);
  fup(i, 1, n - 1) {
    if (i > k)
      break;
    ans += C(n, i) * work(i, n - i);
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fup(i, a, b) for (int i = a; i <= b; ++i)
#define fdw(i, a, b) for (int i = a; i >= b; --i)
#define sync                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
#define cl(a) memset(a, 0, sizeof(a))
#define gcd __gcd
#define fi first
#define se second
#define pb push_back
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll ksm(ll a, ll b, ll mod) {
  ll ans = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
  }
  return ans;
}
// ll oula(ll n){ ll res=n;for(ll i=2;i*i<=n;i++){if(n%i==0){
// res=res-res/i;while(n%i==0) n/=i;}}if(n>1) res=res-res/n;return res;} bool
// MRT(ll x){if(x==2)return true;fup(i,1,50){ll
// now=rand()%(x-2)+2;if(ksm(now,x-1,x)!=1)return false;}return true;} void
// shai(int n)fup(i,2,n){if(isprime[i]){v.push_back(i);for(int
// j=2*i;j<=10000;j+=i)isprime[j]=0;}}//"埃氏筛筛选素数"
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-7;
const int N = 2e5 + 10;
ll fac[N], inv[N];
ll C(ll n, ll m) { return fac[n] * inv[n - m] % mod * inv[m] % mod; }
ll work(ll n, ll m) { return C(n + m - 1, m - 1); }
int main() {
  sync;
  ll n, k;
  ll ans = 1;
  fac[0] = fac[1] = inv[0] = inv[1] = 1;
  fup(i, 2, N - 1) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = -inv[mod % i] * (mod / i) % mod;
    while (inv[i] < 0)
      inv[i] += mod;
  }
  fup(i, 2, N - 1) inv[i] = inv[i] * inv[i - 1] % mod;
  cin >> n >> k;
  k = min(k, n);
  fup(i, 1, n - 1) {
    if (i > k)
      break;
    ans += C(n, i) * work(i, n - i);
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}