Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n1, n2) for (int i = n1; i < n2; i++)
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef long long int ll;
typedef pair<ll, ll> Pi;
const int INF = (ll)(1LL << 31) - 1;
const ll INFl = (ll)9223372036854775807;
const int MAX = 10000;
const ll MOD = (ll)1e9 + 7;
const ll MODI = (ll)1e6 + 3;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int q;
int x[100100], d[100100], n[100100];

ll xnd[1000100LL], xd[1000100LL];
int main() {
  cin >> q;
  REP(i, q) { cin >> x[i] >> d[i] >> n[i]; }
  xnd[0] = 1;
  for (ll i = 1; i <= 1000050; i++) {
    xnd[i] = (xnd[i - 1] * i) % MODI;
  }

  xd[1000002] = mod_pow(xnd[1000002], MODI - 2, MODI);
  for (ll i = 1000002; i > 0; i--) {
    xd[i - 1] = (xd[i] * i % MODI);
  }
  // cout<<"2"<<endl;

  for (int i = 0; i < q; i++) {
    if (d[i] == 0) {
      cout << mod_pow(x[i], n[i], MODI) << endl;
      /*}else
         if(((x[i]*mod_pow(d[i],MODI-2,MODI)-1))/MODI!=(((x[i]*mod_pow(d[i],MODI-2,MODI))+n[i-1]))/MODI){
              cout<<0<<endl;*/
    } else {
      auto ii = x[i] * mod_pow(d[i], MODI - 2, MODI) % MODI;
      if (ii + n[i] - 1 >= MODI || ii == 0)
        cout << 0 << endl;
      // cout<<xd[ii-1]<<endl;
      cout << (xnd[ii + n[i] - 1] * xd[ii - 1] % MODI) *
                  mod_pow(d[i], n[i], MODI) % MODI
           << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n1, n2) for (int i = n1; i < n2; i++)
#define speed_up                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef long long int ll;
typedef pair<ll, ll> Pi;
const int INF = (ll)(1LL << 31) - 1;
const ll INFl = (ll)9223372036854775807;
const int MAX = 10000;
const ll MOD = (ll)1e9 + 7;
const ll MODI = (ll)1e6 + 3;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int q;
int x[100100], d[100100], n[100100];

ll xnd[1000100LL], xd[1000100LL];
int main() {
  cin >> q;
  REP(i, q) { cin >> x[i] >> d[i] >> n[i]; }
  xnd[0] = 1;
  for (ll i = 1; i <= 1000050; i++) {
    xnd[i] = (xnd[i - 1] * i) % MODI;
  }

  xd[1000002] = mod_pow(xnd[1000002], MODI - 2, MODI);
  for (ll i = 1000002; i > 0; i--) {
    xd[i - 1] = (xd[i] * i % MODI);
  }
  // cout<<"2"<<endl;

  for (int i = 0; i < q; i++) {
    if (d[i] == 0) {
      cout << mod_pow(x[i], n[i], MODI) << endl;
      /*}else
         if(((x[i]*mod_pow(d[i],MODI-2,MODI)-1))/MODI!=(((x[i]*mod_pow(d[i],MODI-2,MODI))+n[i-1]))/MODI){
              cout<<0<<endl;*/
    } else {
      auto ii = x[i] * mod_pow(d[i], MODI - 2, MODI) % MODI;
      if (ii + n[i] - 1 >= MODI || ii == 0)
        cout << 0 << endl;
      // cout<<xd[ii-1]<<endl;
      else
        cout << (xnd[ii + n[i] - 1] * xd[ii - 1] % MODI) *
                    mod_pow(d[i], n[i], MODI) % MODI
             << endl;
    }
  }
  return 0;
}