Problem p02990 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
// #include <iomanip>
// #include <string>
#include <vector>
// #include <algorithm>
// #include <utility>
// #include <set>
// #include <map>
// #include <queue>
// #include <deque>
// #include <bitset>
// #include <math.h>
using namespace std;
using ll = long long;
// using ld = long double ;
using vll = vector<ll>;
// using vvll = vector<vll> ;
// using vc = vector<char> ;
// using vvc = vector<vc> ;
// using vb = vector<bool> ;
// using vvb = vector<vb> ;
// using pll = pair<ll,ll> ;

ll mod = 1000000007;
// long double pie = acos(-1) ;

// string yorn(bool a){if(a) return"Yes" ; return "No" ;}
// string YorN(bool a){if(a) return"YES" ; return "NO" ;}
// ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
// ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
// ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}
// void mysort(vector<long long> &a){sort(a.begin(),a.end()) ;}
// void myrev(vector<long long> &a){reverse(a.begin(),a.end()) ;}

ll modpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % mod;
    b >>= 1;
    a = a * a % mod;
  }
  return res;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vll fac(n + 5, 1);
  for (int i = 2; i < n + 5; i++)
    fac.at(i) = fac.at(i - 1) * i % mod;
  n -= k;
  for (int i = 1; i <= k; i++) {
    ll blue = fac.at(k - 1) * modpow(fac.at(k - i), mod - 2) % mod *
              modpow(fac.at(i - 1), mod - 2) % mod;
    ll red = fac.at(n + 1) * modpow(fac.at(n + 1 - i), mod - 2) % mod *
             modpow(fac.at(i), mod - 2) % mod;
    cout << blue * red % mod << endl;
  }
}


Fixed submission:
#include <iostream>
// #include <iomanip>
// #include <string>
#include <vector>
// #include <algorithm>
// #include <utility>
// #include <set>
// #include <map>
// #include <queue>
// #include <deque>
// #include <bitset>
// #include <math.h>
using namespace std;
using ll = long long;
// using ld = long double ;
using vll = vector<ll>;
// using vvll = vector<vll> ;
// using vc = vector<char> ;
// using vvc = vector<vc> ;
// using vb = vector<bool> ;
// using vvb = vector<vb> ;
// using pll = pair<ll,ll> ;

ll mod = 1000000007;
// long double pie = acos(-1) ;

// string yorn(bool a){if(a) return"Yes" ; return "No" ;}
// string YorN(bool a){if(a) return"YES" ; return "NO" ;}
// ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
// ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
// ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}
// void mysort(vector<long long> &a){sort(a.begin(),a.end()) ;}
// void myrev(vector<long long> &a){reverse(a.begin(),a.end()) ;}

ll modpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % mod;
    b >>= 1;
    a = a * a % mod;
  }
  return res;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vll fac(n + 5, 1);
  for (int i = 2; i < n + 5; i++)
    fac.at(i) = fac.at(i - 1) * i % mod;
  n -= k;
  for (int i = 1; i <= k; i++) {
    ll blue = fac.at(k - 1) * modpow(fac.at(k - i), mod - 2) % mod *
              modpow(fac.at(i - 1), mod - 2) % mod;
    if (n + 1 - i < 0) {
      cout << 0 << endl;
      continue;
    }
    ll red = fac.at(n + 1) * modpow(fac.at(n + 1 - i), mod - 2) % mod *
             modpow(fac.at(i), mod - 2) % mod;
    cout << blue * red % mod << endl;
  }
}