Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
const ll inf = (1e18);
const ll mod = 1000003;
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
map<P, ll> mp;
const ll MAX = 2000010; // 設定して

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

// テーブルを作る前処理
void init() {
  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;
  }
}
// 階乗
long long factor(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return (fac[n] * finv[k]) % mod;
}
// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
// aの逆元求めるならn=mod-2
long long modpow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
int main() {
  init();
  int q;
  cin >> q;
  while (q--) {
    ll x, d, n;
    cin >> x >> d >> n;
    if (x == 0)
      cout << 0 << endl;
    else if (d == 0) {
      cout << modpow(x, n) << endl;
    } else if (d == 1) {
      if (n > mod)
        cout << 0 << endl;
      else
        cout << factor(x + n - 1, x - 1) << endl;
    } else {
      x *= modpow(d, mod - 2);
      x %= mod;
      cout << (factor(x + n - 1, x - 1) * modpow(d, n)) % mod << endl;
    }
  }
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
const ll inf = (1e18);
const ll mod = 1000003;
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
map<P, ll> mp;
const ll MAX = 2000010; // 設定して

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

// テーブルを作る前処理
void init() {
  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;
  }
}
// 階乗
long long factor(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return (fac[n] * finv[k]) % mod;
}
// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
// aの逆元求めるならn=mod-2
long long modpow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
int main() {
  init();
  int q;
  cin >> q;
  while (q--) {
    ll x, d, n;
    cin >> x >> d >> n;
    if (x == 0)
      cout << 0 << endl;
    else if (d == 0) {
      cout << modpow(x, n) << endl;
    } else if (n > mod)
      cout << 0 << endl;
    else if (d == 1) {
      cout << factor(x + n - 1, x - 1) << endl;
    } else {
      x *= modpow(d, mod - 2);
      x %= mod;
      cout << (factor(x + n - 1, x - 1) * modpow(d, n)) % mod << endl;
    }
  }
}