Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int mod = 1000003;
/*
class UnionFind{//UnionFind木
 private:
    vector<int> Parent;
 public:
    UnionFind(int N){
        vector<int> Parent(N,-1);
    }

    int root(int A){
        if(Parent[A]<0) return A;
        return Parent[A]=root(Parent[A]);
    }

    int size(int A){
        return -Parent[root(A)];
    }

    bool connect(int A,int B){
        A=root(A);
        B=root(B);
        if(A==B) return false;
        if(size(A)<size(B)) swap(A,B);
        Parent[A]-=Parent[B];
        Parent[B]=A;
    }
};
*/
ll mpow(ll x, ll n) { // x^n(mod division)
                      // ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

ll inv_mod(ll a) { return mpow(a, mod - 2); }

class Fuctorical {
private:
  vector<ll> fac;

public:
  Fuctorical(ll N) {
    fac.push_back(1);
    for (int i = 0; i < N; i++)
      fac.push_back(fac[i] * (i + 1) % mod);
  }

  ll fact(ll a) { return fac[a]; }
  ll ifact(ll a) { return inv_mod(fac[a]); }

  ll cmb(ll a, ll b) {
    if (a == 0 && b == 0)
      return 1;
    if (a < b || a < 0)
      return 0;
    ll tmp = ifact(a - b) * ifact(b) % mod;
    return tmp * fac[a] % mod;
  }
};

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }; // xとyの最大公約数

int main() {
  Fuctorical get(mod);
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    ll x, d, n;
    cin >> x >> d >> n;
    if (d == 0) {
      cout << mpow(x, n) << endl;
      continue;
    }
    ll first = x * inv_mod(d) % mod;
    ll a = get.fact(first + n - 1) * get.ifact(first - 1) % mod;
    cout << a * mpow(d, n) % mod << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int mod = 1000003;
/*
class UnionFind{//UnionFind木
 private:
    vector<int> Parent;
 public:
    UnionFind(int N){
        vector<int> Parent(N,-1);
    }

    int root(int A){
        if(Parent[A]<0) return A;
        return Parent[A]=root(Parent[A]);
    }

    int size(int A){
        return -Parent[root(A)];
    }

    bool connect(int A,int B){
        A=root(A);
        B=root(B);
        if(A==B) return false;
        if(size(A)<size(B)) swap(A,B);
        Parent[A]-=Parent[B];
        Parent[B]=A;
    }
};
*/
ll mpow(ll x, ll n) { // x^n(mod division)
                      // ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

ll inv_mod(ll a) { return mpow(a, mod - 2); }

class Fuctorical {
private:
  vector<ll> fac;

public:
  Fuctorical(ll N) {
    fac.push_back(1);
    for (int i = 0; i < N; i++)
      fac.push_back(fac[i] * (i + 1) % mod);
  }

  ll fact(ll a) { return fac[a]; }
  ll ifact(ll a) { return inv_mod(fac[a]); }

  ll cmb(ll a, ll b) {
    if (a == 0 && b == 0)
      return 1;
    if (a < b || a < 0)
      return 0;
    ll tmp = ifact(a - b) * ifact(b) % mod;
    return tmp * fac[a] % mod;
  }
};

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }; // xとyの最大公約数

int main() {
  Fuctorical get(mod);
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    ll x, d, n;
    cin >> x >> d >> n;
    if (d == 0) {
      cout << mpow(x, n) << endl;
      continue;
    }
    ll first = x * inv_mod(d) % mod;
    if (first <= mod && mod <= first + n - 1) {
      cout << 0 << endl;
      continue;
    }
    ll a = get.fact(first + n - 1) * get.ifact(first - 1) % mod;
    cout << a * mpow(d, n) % mod << endl;
  }
}