Problem p02625 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define sz(v) (ll)(v).size()
#define inf 1e18
#define md 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define tel(a)                                                                 \\
  { cout << a << "\\n"; }
#define tell(a, b)                                                             \\
  { cout << a << " | " << b << "\\n"; }
#define telll(a, b, c)                                                         \\
  { cout << a << " | " << b << " | " << c << "\\n"; }
#define teln(v, n)                                                             \\
  {                                                                            \\
    cout << "v- ";                                                             \\
    rep(o, 0, n) cout << v[o] << " ";                                          \\
    cout << "\\n";                                                              \\
  }
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define M 500010
ll ex(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % md;
    a = (a * a) % md, b /= 2;
  }
  return ans;
}
inline ll add(ll a, ll b) {
  a = a % md + b % md;
  if (a > md)
    a %= md;
  return a;
}
inline ll sub(ll a, ll b) {
  a = a % md - b % md;
  if (a < 0)
    a += md;
  return a;
}
inline ll mul(ll a, ll b) {
  a %= md, b %= md;
  return (a * b) % md;
}

ll f[M], iv[M];
ll C(ll n, ll r) {
  if (n < r)
    return 0;
  return mul(f[n], mul(iv[r], iv[n - r]));
}

void prep_fac() {
  f[0] = 1;
  rep(i, 1, M) f[i] = mul(i, f[i - 1]);
  iv[M - 1] = ex(f[M - 1], md - 2);
  for (ll i = M - 2; i >= 0; --i)
    iv[i] = mul(i + 1, iv[i + 1]);
}

// common=x  C(m,x)C(m-x,n-x)C(m-x-(n-x),n-x)
// 12 13 23 21 31 32
// n!-C(x,1)(n-1)!+C(x,2)(n-2)!-..
// n!-(x+1)(n-1)!/1!+(x+1)x(n-2)!/2!
// C(n,x)C(x,i)=n!/(n-x)!i!(x-i)! =C(n,i)C(n-i,n-x)
// C(m,n)n!sum x-0,n i>=0 (-1)^i C(n,x) C(m-n,n-x) C(x,i)(n-i)! =
//  C(n,i)  sum x-0,n C(m-n,n-x) C(n-i,x-i) (n-i)!^2 = C(n,i) C(m-i,n-i) (n-i)!
int main() {
  IOS;

  prep_fac();
  ll n, m;
  cin >> n >> m;
  ll ans = 0, ok = 1;
  rep(i, 0, n + 1) {
    ans = (ans + ok * mul(C(n, i), mul(C(m - i, n - i), f[n - i]))) % md;
    (ans += md) %= md;
    ok *= -1;
  }
  ans = mul(ans, mul(C(m, n), f[n]));
  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define sz(v) (ll)(v).size()
#define inf 1e18
#define md 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define tel(a)                                                                 \\
  { cout << a << "\\n"; }
#define tell(a, b)                                                             \\
  { cout << a << " | " << b << "\\n"; }
#define telll(a, b, c)                                                         \\
  { cout << a << " | " << b << " | " << c << "\\n"; }
#define teln(v, n)                                                             \\
  {                                                                            \\
    cout << "v- ";                                                             \\
    rep(o, 0, n) cout << v[o] << " ";                                          \\
    cout << "\\n";                                                              \\
  }
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define M 1000010
ll ex(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % md;
    a = (a * a) % md, b /= 2;
  }
  return ans;
}
inline ll add(ll a, ll b) {
  a = a % md + b % md;
  if (a > md)
    a %= md;
  return a;
}
inline ll sub(ll a, ll b) {
  a = a % md - b % md;
  if (a < 0)
    a += md;
  return a;
}
inline ll mul(ll a, ll b) {
  a %= md, b %= md;
  return (a * b) % md;
}

ll f[M], iv[M];
ll C(ll n, ll r) {
  if (n < r)
    return 0;
  return mul(f[n], mul(iv[r], iv[n - r]));
}

void prep_fac() {
  f[0] = 1;
  rep(i, 1, M) f[i] = mul(i, f[i - 1]);
  iv[M - 1] = ex(f[M - 1], md - 2);
  for (ll i = M - 2; i >= 0; --i)
    iv[i] = mul(i + 1, iv[i + 1]);
}

int main() {
  IOS;

  prep_fac();
  ll n, m;
  cin >> n >> m;
  ll ans = 0;
  rep(x, 0, n + 1) {
    if (m - x < 0)
      continue;
    ll w = mul(C(m, x), C(m - x, n - x));
    w = mul(w, C(m - n, n - x));
    ll w2 = f[n], ok = -1;
    rep(i, 1, x + 1) {
      w2 = (w2 + ok * mul(C(x, i), f[n - i])) % md;
      (w2 += md) %= md;
      ok *= -1;
    }
    w2 = mul(w2, f[n]);
    ans += mul(w, w2);
  }
  cout << ans;

  return 0;
}