Problem p02625 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll inv(ll x) {
  // MOD=10e9+7
  ll n = MOD - 2, a = 1;
  while (n > 0) {
    if (n % 2)
      a = a * x % MOD;
    x = x * x % MOD;
    n = n / 2;
  }
  return a;
}

vector<ll> facts(1, 1);
ll factorial(ll n) {
  if (facts.size() - 1 >= n)
    return facts[n];
  else {
    rep(i, facts.size(), n + 1) { facts.push_back((facts[i - 1] * i) % MOD); }
    return facts[n];
  }
}

vector<ll> inv_facts(1, 1);
ll inv_factorial(ll n) {
  if (inv_facts.size() - 1 >= n)
    return inv_facts[n];
  else {
    rep(i, inv_facts.size(), n + 1) {
      inv_facts.push_back((inv_facts[i - 1] * inv(i)) % MOD);
    }
    return inv_facts[n];
  }
}

ll Comb(ll n, ll m) {
  return (((factorial(n) * inv_factorial(m)) % MOD) * inv_factorial(n - m)) %
         MOD;
}

ll PowMod(ll N, ll P, ll M) {
  ll ans = 1;
  while (P >= 1) {
    if (P % 2)
      ans = ans * N % M;
    N = N * N % M;
    P /= 2;
  }
  ans %= M;
  return ans;
}

int main() {
  ll N, M;
  cin >> N >> M;
  ll ans = 0;
  rep(k, 0, N + 1) {
    if (k % 2 == 0)
      ans += (((factorial(M - k) * inv_factorial(M - N)) % MOD) * Comb(N, k)) %
             MOD;
    else {
      ans -=
          ((factorial(M - k) * inv_factorial(M - N)) % MOD * Comb(N, k)) % MOD;
      ans += MOD;
      ans %= MOD;
    }
  }
  ans *= factorial(M);
  ans %= MOD;
  ans *= inv_factorial(M - N);
  ans %= MOD;
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll inv(ll x) {
  // MOD=10e9+7
  ll n = MOD - 2, a = 1;
  while (n > 0) {
    if (n % 2)
      a = a * x % MOD;
    x = x * x % MOD;
    n = n / 2;
  }
  return a;
}

vector<ll> facts(1, 1);
ll factorial(ll n) {
  if (facts.size() - 1 >= n)
    return facts[n];
  else {
    rep(i, facts.size(), n + 1) { facts.push_back((facts[i - 1] * i) % MOD); }
    return facts[n];
  }
}

vector<ll> inv_facts(1, 1);
ll inv_factorial(ll n) {
  if (inv_facts.size() - 1 >= n)
    return inv_facts[n];
  else {
    rep(i, inv_facts.size(), n + 1) {
      inv_facts.push_back((inv_facts[i - 1] * inv(i)) % MOD);
    }
    return inv_facts[n];
  }
}

ll Comb(ll n, ll m) {
  return (((factorial(n) * inv_factorial(m)) % MOD) * inv_factorial(n - m)) %
         MOD;
}

ll PowMod(ll N, ll P, ll M) {
  ll ans = 1;
  while (P >= 1) {
    if (P % 2)
      ans = ans * N % M;
    N = N * N % M;
    P /= 2;
  }
  ans %= M;
  return ans;
}

int main() {
  ll N, M;
  cin >> M >> N;
  ll ans = 0;
  rep(k, 0, N + 1) {
    if (k % 2 == 0)
      ans += (((factorial(M - k) * inv_factorial(M - N)) % MOD) * Comb(N, k)) %
             MOD;
    else {
      ans -=
          ((factorial(M - k) * inv_factorial(M - N)) % MOD * Comb(N, k)) % MOD;
      ans += MOD;
      ans %= MOD;
    }
  }
  ans *= factorial(M);
  ans %= MOD;
  ans *= inv_factorial(M - N);
  ans %= MOD;
  cout << ans;
  return 0;
}