Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#ifdef ONLINE_JUDGE
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
#define fu(a, b, c) for (int a = b; a <= (int)c; ++a)
#define fd(a, b, c) for (int a = b; a >= (int)c; --a)
#define cu(a, b, c) for (int a = b; a < (int)c; ++a)
#define cd(a, b, c) for (int a = (b)-1; a >= (int)c; --a)
#define gcd(a, b) __gcd(a, b)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define pf push_front
#define MOD 1000003
#define MAXN 52
#define MAXH 20
#define MAXM 52
#define INF 100000000
#define ll long long
#define ull unsigned long long
#define RNG rng mt19937(time(NULL))
#define udist(a, b) uniform_int_distribution<uint32_t> distribution(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define gmax(a, b) a = max(a, b)
#define gmin(a, b) a = min(a, b)
#define whatis(x) cerr << #x << " is " << x << endl;
#define fast_io()                                                              \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define PI 3.1415926535897932384626433
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
const int N = 2000010;
ll t[N << 1];
int sze;

void init(int n) {
  sze = n;
  cu(i, 0, n) t[i + n] = i + 1;
  cd(i, n, 1) { t[i] = t[i << 1] * t[i << 1 | 1] % MOD; }
}

ll query(int l, int r) {
  ll res = 1;
  for (l += sze, r += sze; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = res * t[l++] % MOD;
    if (r & 1)
      res = res * t[--r] % MOD;
  }
  return res;
}

ll modpow(ll x, ll p) {
  if (p == 0)
    return 1;
  ll y = 1;
  while (p > 1) {
    if (p & 1)
      y = y * x % MOD;
    x = x * x % MOD;
    p >>= 1;
  }
  return x * y % MOD;
}

int main() {
  fast_io();
  init(MOD * 2);
  int q;
  cin >> q;
  cu(i, 0, q) {
    ll x, d, n;
    cin >> x >> d >> n;
    ll ans = modpow(d, n);
    ll start = x * modpow(d, MOD - 2) % MOD;
    ans *= query(start - 1, start + n - 1);
    ans %= MOD;
    cout << ans << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#ifdef ONLINE_JUDGE
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
#define fu(a, b, c) for (int a = b; a <= (int)c; ++a)
#define fd(a, b, c) for (int a = b; a >= (int)c; --a)
#define cu(a, b, c) for (int a = b; a < (int)c; ++a)
#define cd(a, b, c) for (int a = (b)-1; a >= (int)c; --a)
#define gcd(a, b) __gcd(a, b)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define pf push_front
#define MOD 1000003
#define MAXN 52
#define MAXH 20
#define MAXM 52
#define INF 100000000
#define ll long long
#define ull unsigned long long
#define RNG rng mt19937(time(NULL))
#define udist(a, b) uniform_int_distribution<uint32_t> distribution(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define gmax(a, b) a = max(a, b)
#define gmin(a, b) a = min(a, b)
#define whatis(x) cerr << #x << " is " << x << endl;
#define fast_io()                                                              \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define PI 3.1415926535897932384626433
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
const int N = 2000010;
ll t[N << 1];
int sze;

void init(int n) {
  sze = n;
  cu(i, 0, n) t[i + n] = i + 1;
  cd(i, n, 1) { t[i] = t[i << 1] * t[i << 1 | 1] % MOD; }
}

ll query(int l, int r) {
  ll res = 1;
  for (l += sze, r += sze; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = res * t[l++] % MOD;
    if (r & 1)
      res = res * t[--r] % MOD;
  }
  return res;
}

ll modpow(ll x, ll p) {
  if (p == 0)
    return 1;
  ll y = 1;
  while (p > 1) {
    if (p & 1)
      y = y * x % MOD;
    x = x * x % MOD;
    p >>= 1;
  }
  return x * y % MOD;
}

int main() {
  fast_io();
  init(MOD * 2);
  int q;
  cin >> q;
  cu(i, 0, q) {
    ll x, d, n;
    cin >> x >> d >> n;
    if (d == 0) {
      cout << modpow(x, n) << '\n';
    } else if (n >= MOD) {
      cout << 0 << '\n';
    } else {
      ll ans = modpow(d, n);
      ll start = x * modpow(d, MOD - 2) % MOD;
      ans *= query(start - 1, start + n - 1);
      ans %= MOD;
      cout << ans << '\n';
    }
  }
}