Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 10000000;
const int MOD = 1000003;

long long fac[MAX], finv[MAX], inv[MAX];

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

int main() {
  vector<ll> ans;
  // 前処理
  COMinit();
  ll Q;
  cin >> Q;
  ll i, j, k, l;
  rep(i, Q) {
    ll x, d, n;
    cin >> x >> d >> n;
    ll tmp;
    if (d == 0) {
      tmp = modpow(x, n, MOD);
      ans.pb(tmp);
    } else {
      if (n >= MAX) {
        ans.pb(0);
      } else {
        x = x * inv[d] % MOD;
        tmp = fac[x + n - 1] * finv[x - 1] % MOD * modpow(d, n, MOD) % MOD;
        ans.pb(tmp);
      }
    }
  }
  rep(i, ans.size()) cout << ans.at(i) << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 10000000;
const int MOD = 1000003;

long long fac[MAX], finv[MAX], inv[MAX];

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

int main() {
  vector<ll> ans;
  // 前処理
  COMinit();
  ll Q;
  cin >> Q;
  ll i, j, k, l;
  rep(i, Q) {
    ll x, d, n;
    cin >> x >> d >> n;
    ll tmp;
    if (d == 0) {
      tmp = modpow(x, n, MOD);
      ans.pb(tmp);
    } else {
      if (n >= MOD) {
        ans.pb(0);
      } else {
        x = x * inv[d] % MOD;
        tmp = fac[x + n - 1] * finv[x - 1] % MOD * modpow(d, n, MOD) % MOD;
        ans.pb(tmp);
      }
    }
  }
  rep(i, ans.size()) cout << ans.at(i) << endl;
}