Problem p02769 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
#define int __int128
inline int gi() {
  short tmp = getchar();
  int flag = 1;
  while (!isdigit(tmp)) {
    if (tmp == '-') {
      flag = -1;
      tmp = getchar();
      break;
    }
    tmp = getchar();
  }
  int ans = 0;
  while (isdigit(tmp)) {
    ans = (ans << 3) + (ans << 1) + tmp - '0';
    tmp = getchar();
  }
  return ans * flag;
}
inline void write(int x) {
  static int stk[100], top = 0;
  if (x == 0) {
    putchar('0');
    putchar(' ');
    return;
  }
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  while (x) {
    stk[++top] = x % 10;
    x /= 10;
  }
  while (top) {
    putchar(stk[top--] + '0');
  }
  putchar(' ');
}
#define line() putchar('\n');
#define Mem(Arr, V) memset(Arr, V, sizeof Arr);
#define Mcpy(Arr, qwq) memcpy(Arr, qwq, sizeof qwq);
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max4(max3(a, b, c), d);
#define in(a) a = gi()
#define in2(a, b) in(a), in(b)
#define in3(a, b, c) in2(a, b), in(c)
#define in4(a, b, c, d) in3(a, b, c), in(d)
#define write2(a, b) write(a), write(b)
#define write3(a, b, c) write2(a, b), write(c)
#define write4(a, b, c, d) write3(a, b, c), write(d)
inline void smin(int &x, int y) { x = min(x, y); }
inline void smax(int &x, int y) { x = max(x, y); }
const int inf = 8000000;
const int Mod = 1e9 + 7;
inline int Pow(int b, int p, int k) {
  int ans = 1;
  while (p) {
    if (p & 1) {
      ans = ans * b % k;
    }
    b = b * b % k;
    p >>= 1;
  }
  return ans;
}
inline int Inv(int a) { return (Pow(a, Mod - 2, Mod)); }
const int N = 2000000;
int Fac[N];
inline void Prepare() {
  Fac[0] = 1;
  for (int i = 1; i <= 1000000; ++i)
    Fac[i] = i * Fac[i - 1] % Mod;
}
inline int C(int n, int m) { return Fac[n] * Inv(Fac[n - m] * Fac[m]) % Mod; }
signed main() {
  freopen("data.in", "r", stdin);
  int n, k;
  in2(n, k);
  Prepare();
  if (n <= k) {
    write(C(n + n - 1, n - 1));
  } else {
    int all = C(n + n - 1, n - 1);
    for (int i = 1; i < n - k; ++i) {
      all -= C(n, i) * C(n - 1, i - 1) % Mod;
      all = (all % Mod + Mod) % Mod;
    }
    write(all);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
#define int __int128
inline int gi() {
  short tmp = getchar();
  int flag = 1;
  while (!isdigit(tmp)) {
    if (tmp == '-') {
      flag = -1;
      tmp = getchar();
      break;
    }
    tmp = getchar();
  }
  int ans = 0;
  while (isdigit(tmp)) {
    ans = (ans << 3) + (ans << 1) + tmp - '0';
    tmp = getchar();
  }
  return ans * flag;
}
inline void write(int x) {
  static int stk[100], top = 0;
  if (x == 0) {
    putchar('0');
    putchar(' ');
    return;
  }
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  while (x) {
    stk[++top] = x % 10;
    x /= 10;
  }
  while (top) {
    putchar(stk[top--] + '0');
  }
  putchar(' ');
}
#define line() putchar('\n');
#define Mem(Arr, V) memset(Arr, V, sizeof Arr);
#define Mcpy(Arr, qwq) memcpy(Arr, qwq, sizeof qwq);
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max4(max3(a, b, c), d);
#define in(a) a = gi()
#define in2(a, b) in(a), in(b)
#define in3(a, b, c) in2(a, b), in(c)
#define in4(a, b, c, d) in3(a, b, c), in(d)
#define write2(a, b) write(a), write(b)
#define write3(a, b, c) write2(a, b), write(c)
#define write4(a, b, c, d) write3(a, b, c), write(d)
inline void smin(int &x, int y) { x = min(x, y); }
inline void smax(int &x, int y) { x = max(x, y); }
const int inf = 8000000;
const int Mod = 1e9 + 7;
inline int Pow(int b, int p, int k) {
  int ans = 1;
  while (p) {
    if (p & 1) {
      ans = ans * b % k;
    }
    b = b * b % k;
    p >>= 1;
  }
  return ans;
}
inline int Inv(int a) { return (Pow(a, Mod - 2, Mod)); }
const int N = 2000000;
int Fac[N];
inline void Prepare() {
  Fac[0] = 1;
  for (int i = 1; i <= 1000000; ++i)
    Fac[i] = i * Fac[i - 1] % Mod;
}
inline int C(int n, int m) { return Fac[n] * Inv(Fac[n - m] * Fac[m]) % Mod; }
signed main() {
  int n, k;
  in2(n, k);
  Prepare();
  if (n <= k) {
    write(C(n + n - 1, n - 1));
  } else {
    int all = C(n + n - 1, n - 1);
    for (int i = 1; i < n - k; ++i) {
      all -= C(n, i) * C(n - 1, i - 1) % Mod;
      all = (all % Mod + Mod) % Mod;
    }
    write(all);
  }
  return 0;
}