Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(x, y, h, w) x >= 0 && x < h &&y >= 0 && y < w

#define int long long
// typedef    long long          ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> P;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

const int INF = 1e+18;
const double EPS = 1e-9;
const int MOD = 1000003;

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

int mod_inv(int X) {
  int a = X, b = MOD, u = 1, v = 0;
  while (b) {
    int t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  if (u < 0)
    u += MOD;
  return u;
}

vector<int> fact, factinv;

void nCr_init(int N) {
  N = min(N, (int)MOD);
  fact.resize(N + 1);
  factinv.resize(N + 1);
  fact[0] = 1;
  for (int i = 1; i <= N; i++)
    fact[i] = fact[i - 1] * i % MOD;
  factinv[N] = mod_inv(fact[N]);
  for (int i = N; i >= 1; i--)
    factinv[i - 1] = factinv[i] * i % MOD;
}

int nCr(int ncr_n, int ncr_r) {
  if (ncr_n >= MOD)
    return nCr(ncr_n % MOD, ncr_r % MOD) * nCr(ncr_n / MOD, ncr_r / MOD) % MOD;
  return ncr_r > ncr_n ? 0
                       : fact[ncr_n] * factinv[ncr_n - ncr_r] % MOD *
                             factinv[ncr_r] % MOD;
}

int pow2(int a, int b) {
  if (!b)
    return 1;
  int tmp = pow2(a * a % MOD, b / 2);
  if (b % 2)
    tmp = (tmp * a) % MOD;
  return tmp;
}

signed main() {
  int q;
  cin >> q;
  nCr_init(MOD - 1);
  for (int i = 0; i < q; i++) {
    int x, d, n;
    cin >> x >> d >> n;
    cout << fact[n] * nCr(MOD - x * mod_inv(d) % MOD, n) % MOD *
                pow2(MOD - d, n) % MOD
         << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(x, y, h, w) x >= 0 && x < h &&y >= 0 && y < w

#define int long long
// typedef    long long          ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> P;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

const int INF = 1e+18;
const double EPS = 1e-9;
const int MOD = 1000003;

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

int mod_inv(int X) {
  int a = X, b = MOD, u = 1, v = 0;
  while (b) {
    int t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  if (u < 0)
    u += MOD;
  return u;
}

vector<int> fact, factinv;

void nCr_init(int N) {
  N = min(N, (int)MOD);
  fact.resize(N + 1);
  factinv.resize(N + 1);
  fact[0] = 1;
  for (int i = 1; i <= N; i++)
    fact[i] = fact[i - 1] * i % MOD;
  factinv[N] = mod_inv(fact[N]);
  for (int i = N; i >= 1; i--)
    factinv[i - 1] = factinv[i] * i % MOD;
}

int nCr(int ncr_n, int ncr_r) {
  if (ncr_n >= MOD)
    return nCr(ncr_n % MOD, ncr_r % MOD) * nCr(ncr_n / MOD, ncr_r / MOD) % MOD;
  return ncr_r > ncr_n ? 0
                       : fact[ncr_n] * factinv[ncr_n - ncr_r] % MOD *
                             factinv[ncr_r] % MOD;
}

int pow2(int a, int b) {
  if (!b)
    return 1;
  int tmp = pow2(a * a % MOD, b / 2);
  if (b % 2)
    tmp = (tmp * a) % MOD;
  return tmp;
}

signed main() {
  int q;
  cin >> q;
  nCr_init(MOD - 1);
  for (int i = 0; i < q; i++) {
    int x, d, n;
    cin >> x >> d >> n;
    if (!d)
      cout << pow2(x, n) << endl;
    else if (n >= MOD)
      cout << 0 << endl;
    else
      cout << fact[n] * nCr(MOD - x * mod_inv(d) % MOD, n) % MOD *
                  pow2(MOD - d, n) % MOD
           << endl;
  }
}