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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef tree<int,null_type,less<int
// >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v);
#ifndef ONLINE_JUDGE
#define debug(a) cout << #a << " --> " << (a) << "\\n";
#define debug2(a) cout << #a << " --> " << (a) << " ";
#define HERE cout << "here - " << __LINE__ << "\\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 1e9 + 7;
const int N = 5e5 + 5;
typedef pair<int, int> pii;

ll bigmod(ll a, ll b) {
  a %= mod;
  ll ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
ll fact[N];
void init() {
  fact[0] = 1;
  for (ll i = 1; i < N; i++)
    fact[i] = (fact[i - 1] * i) % mod;
}
ll getncr(ll n, ll r) {
  if (n < r) {
    // cout<<n<<" "<<r<<endl;
    return 0;
  }
  ll up = fact[n];
  ll down = (fact[r] * fact[n - r]) % mod;
  return (up * bigmod(down, mod - 2)) % mod;
}
ll add(ll x, ll y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
ll sub(ll x, ll y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
ll gun(ll x, ll y) { return (x * y) % mod; }
ll vag(ll x, ll y) {
  x = x * bigmod(y, mod - 2);
  return x % mod;
}

void TEST_CASES(int cas) {
  init();
  ll n, m;
  cin >> n >> m;
  ll ans = 0;
  for (ll i = 0; i <= n; i++) {
    ll t = getncr(n, i);
    t = gun(t, getncr(m - i, n - i));
    t = gun(t, fact[n - i]);
    if (i % 2 == 0) {
      ans = add(ans, t);
    } else {
      ans = sub(ans, t);
    }
  }
  ans = gun(ans, gun(getncr(m, n), fact[n]));
  printf("%lld\\n", ans);
}

/*

*/

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int t = 1, cas = 0;
  // scanf("%d",&t);
  while (t--) {
    TEST_CASES(++cas);
  }
  return 0;
}

// os.order_of_key(v): returns how many elements strictly less than v
// os.find_by_order()

template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}

template <class T> ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) {
  for (auto i : v) {
    os << "Key : " << i.first << " , Value : " << i.second << endl;
  }
  return os;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef tree<int,null_type,less<int
// >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v);
#ifndef ONLINE_JUDGE
#define debug(a) cout << #a << " --> " << (a) << "\\n";
#define debug2(a) cout << #a << " --> " << (a) << " ";
#define HERE cout << "here - " << __LINE__ << "\\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 1e9 + 7;
const int N = 5e5 + 5;
typedef pair<int, int> pii;

ll bigmod(ll a, ll b) {
  a %= mod;
  ll ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
ll fact[N];
void init() {
  fact[0] = 1;
  for (ll i = 1; i < N; i++)
    fact[i] = (fact[i - 1] * i) % mod;
}
ll getncr(ll n, ll r) {
  if (n < r) {
    // cout<<n<<" "<<r<<endl;
    return 0;
  }
  ll up = fact[n];
  ll down = (fact[r] * fact[n - r]) % mod;
  return (up * bigmod(down, mod - 2)) % mod;
}
ll add(ll x, ll y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
ll sub(ll x, ll y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
ll gun(ll x, ll y) { return (x * y) % mod; }
ll vag(ll x, ll y) {
  x = x * bigmod(y, mod - 2);
  return x % mod;
}
ll fun(ll n, ll k) {
  ll ret = fact[n];
  ll now = -1;
  for (ll i = 1; i <= k; i++) {
    ll t = getncr(k, i);
    t = gun(t, fact[n - i]);
    if (now == -1) {
      ret = sub(ret, t);
    } else {
      ret = add(ret, t);
    }
    now *= -1;
  }
  return ret;
}
void TEST_CASES(int cas) {
  init();
  ll n, m;
  cin >> n >> m;
  ll ans = 0;
  for (ll i = 0; i <= m - n; i++) {
    if (i > n)
      break;
    ll t = getncr(m, n);
    t = gun(t, fact[n]);
    t = gun(t, getncr(n, n - i));
    t = gun(t, getncr(m - n, i));
    t = gun(t, fun(n, n - i));
    ans = add(ans, t);
  }
  printf("%lld\\n", ans);
}

/*

*/

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int t = 1, cas = 0;
  // scanf("%d",&t);
  while (t--) {
    TEST_CASES(++cas);
  }
  return 0;
}

// os.order_of_key(v): returns how many elements strictly less than v
// os.find_by_order()

template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}

template <class T> ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) {
  for (auto i : v) {
    os << "Key : " << i.first << " , Value : " << i.second << endl;
  }
  return os;
}