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

// In god we trust

#include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <signal.h>
#include <thread>

#define pb push_back
#define fi first
#define se second
#define Vi vector<int>
#define Vb vector<bool>
#define Pi pair<int, int>
#define MP make_pair
#define MT make_tuple
#define Q queue
#define Qi Q<int>
#define INF 1000000000
#define LINF 1000000000000000000
#define int long long
using namespace std;
using namespace std::chrono;

/* Iterative Function to calculate (x^y)%p
  in O(log y) */
int power(int x, int y, int p) {
  int res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

long modPow(long a, long x, long p) {
  // calculates a^x mod p in logarithmic time.
  long res = 1;
  while (x > 0) {
    if (x % 2 != 0) {
      res = (res * a) % p;
    }
    a = (a * a) % p;
    x /= 2;
  }
  return res;
}

long modInverse(long a, long p) {
  // calculates the modular multiplicative of a mod m.
  //(assuming p is prime).
  return modPow(a, p - 2, p);
}

int numerator = -1;
int denominator = -1;

long modBinomial(long n, long k, long p) {
  // calculates C(n,k) mod p (assuming p is prime).

  if (numerator == -1) {
    numerator = 1;
    for (int i = 0; i < k; i++) {
      numerator = (numerator * (n - i)) % p;
    }
  } else
    numerator = numerator * (n - (k - 1)) % p;

  if (denominator == -1) {
    denominator = 1; // k!
    for (int i = 1; i <= k; i++) {
      denominator = (denominator * i) % p;
    }
  } else {
    denominator = (denominator * k) % p;
  }

  // numerator / denominator mod p.
  return (numerator * modInverse(denominator, p)) % p;
}

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

  int mod = 998244353;
  int n, m, k;
  cin >> n >> m >> k;

  int ans = 0;
  for (int i = 0; i <= k; i++)
    ans += (((m * modBinomial(n - 1, i, mod)) % mod) *
            power(m - 1, n - (i + 1), mod) % mod),
        ans %= mod;

  cout << ans << endl;
}


Error: TLE

Fixed submission:

// In god we trust

#include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <signal.h>
#include <thread>

#define pb push_back
#define fi first
#define se second
#define Vi vector<int>
#define Vb vector<bool>
#define Pi pair<int, int>
#define MP make_pair
#define MT make_tuple
#define Q queue
#define Qi Q<int>
#define INF 1000000000
#define LINF 1000000000000000000
#define int long long
using namespace std;
using namespace std::chrono;

/* Iterative Function to calculate (x^y)%p
  in O(log y) */
int power(int x, int y, int p) {
  int res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

long modPow(long a, long x, long p) {
  // calculates a^x mod p in logarithmic time.
  long res = 1;
  while (x > 0) {
    if (x % 2 != 0) {
      res = (res * a) % p;
    }
    a = (a * a) % p;
    x /= 2;
  }
  return res;
}

long modInverse(long a, long p) {
  // calculates the modular multiplicative of a mod m.
  //(assuming p is prime).
  return modPow(a, p - 2, p);
}
long modBinomial(long n, long k, long p) {
  // calculates C(n,k) mod p (assuming p is prime).

  long numerator = 1; // n * (n-1) * ... * (n-k+1)
  for (int i = 0; i < k; i++) {
    numerator = (numerator * (n - i)) % p;
  }

  long denominator = 1; // k!
  for (int i = 1; i <= k; i++) {
    denominator = (denominator * i) % p;
  }

  // numerator / denominator mod p.
  return (numerator * modInverse(denominator, p)) % p;
}

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

  int mod = 998244353;
  int n, m, k;
  cin >> n >> m >> k;

  int ans = 0;
  for (int i = 0; i <= k; i++)
    ans += (((m * modBinomial(n - 1, i, mod)) % mod) *
            power(m - 1, n - (i + 1), mod) % mod),
        ans %= mod;

  cout << ans << endl;
}