Problem p02625 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define ld long double
#define mod 1000000007
#define big 1e18 + 10
#define small -big
#define pb push_back
#define endl "\n"

template <typename T>

void prarr(T a) {
  cerr << "[ ";

  for (auto i : a) {
    cerr << i << " ";
  }

  cerr << "]" << endl;
}

#define trace(...) _er(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>

void _er(const char *name, Arg1 &&arg1) {
  cerr << "[" << name << " : " << arg1 << "]" << endl;
}

template <typename Arg1, typename... Args>

void _er(const char *names, Arg1 &&arg1, Args &&...args) {

  const char *comma = strchr(names + 1, ',');
  cerr << "[";
  cerr.write(names, comma - names) << " : " << arg1 << "] ";
  _er(comma + 2, args...);
}

ll pow(ll a, ll n, ll m) {

  ll res = 1;

  while (n) {

    if (n % 2)
      res = res * a % m;
    a = a * a % m;
    n /= 2;
  }

  return res;
}

ll Fact[1000005];
ll invFact[1000005];

void cal() {

  Fact[0] = 1;
  Fact[1] = 1;

  for (ll i = 2; i <= 1000000; i++) {

    Fact[i] = i * Fact[i - 1] % mod;
  }

  invFact[0] = 1;

  for (ll i = 1; i <= 1000000; i++) {

    invFact[i] = pow(Fact[i], mod - 2, mod);
  }
}

ll comb(ll n, ll r) {

  ll res = 1;

  res = Fact[n];
  res = res * invFact[n - r] % mod;
  res = res * invFact[r] % mod;

  return res;
}

void addse(ll &a, ll b) {
  a %= mod;
  b %= mod;
  a += b;
  a %= mod;
}

void subse(ll &a, ll b) {
  a %= mod;
  b %= mod;
  a -= b;
  if (a < 0) {
    a += mod;
  }

  a %= mod;
}

void mulse(ll &a, ll b) {
  a %= mod;
  b %= mod;
  a *= b;
  a %= mod;
}

void solve() {

  ll N, M;
  cin >> N >> M;

  ll tot = 0;

  for (ll i = 0; i <= N; i++) {
    ll now = comb(N, i);
    mulse(now, comb(M, i));
    mulse(now, Fact[i]);
    mulse(now, comb(M - i, N - i));
    mulse(now, Fact[N - i]);
    mulse(now, comb(M - i, N - i));
    mulse(now, Fact[N - i]);
    trace(i, now);
    if (i % 2) {
      subse(tot, now);
    } else {
      addse(tot, now);
    }
  }

  cout << tot << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cal();

  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define ld long double
#define mod 1000000007
#define big 1e18 + 10
#define small -big
#define pb push_back
#define endl "\n"

template <typename T>

void prarr(T a) {
  cerr << "[ ";

  for (auto i : a) {
    cerr << i << " ";
  }

  cerr << "]" << endl;
}

#define trace(...) _er(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>

void _er(const char *name, Arg1 &&arg1) {
  cerr << "[" << name << " : " << arg1 << "]" << endl;
}

template <typename Arg1, typename... Args>

void _er(const char *names, Arg1 &&arg1, Args &&...args) {

  const char *comma = strchr(names + 1, ',');
  cerr << "[";
  cerr.write(names, comma - names) << " : " << arg1 << "] ";
  _er(comma + 2, args...);
}

ll pow(ll a, ll n, ll m) {

  ll res = 1;

  while (n) {

    if (n % 2)
      res = res * a % m;
    a = a * a % m;
    n /= 2;
  }

  return res;
}

ll Fact[1000005];
ll invFact[1000005];

void cal() {

  Fact[0] = 1;
  Fact[1] = 1;

  for (ll i = 2; i <= 1000000; i++) {

    Fact[i] = i * Fact[i - 1] % mod;
  }

  invFact[0] = 1;

  for (ll i = 1; i <= 1000000; i++) {

    invFact[i] = pow(Fact[i], mod - 2, mod);
  }
}

ll comb(ll n, ll r) {

  ll res = 1;

  res = Fact[n];
  res = res * invFact[n - r] % mod;
  res = res * invFact[r] % mod;

  return res;
}

void addse(ll &a, ll b) {
  a %= mod;
  b %= mod;
  a += b;
  a %= mod;
}

void subse(ll &a, ll b) {
  a %= mod;
  b %= mod;
  a -= b;
  if (a < 0) {
    a += mod;
  }

  a %= mod;
}

void mulse(ll &a, ll b) {
  a %= mod;
  b %= mod;
  a *= b;
  a %= mod;
}

void solve() {

  ll N, M;
  cin >> N >> M;

  ll tot = 0;

  for (ll i = 0; i <= N; i++) {
    ll now = comb(N, i);
    mulse(now, comb(M, i));
    mulse(now, Fact[i]);
    mulse(now, comb(M - i, N - i));
    mulse(now, Fact[N - i]);
    mulse(now, comb(M - i, N - i));
    mulse(now, Fact[N - i]);

    if (i % 2) {
      subse(tot, now);
    } else {
      addse(tot, now);
    }
  }

  cout << tot << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cal();

  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }

  return 0;
}