Problem p03027 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define start_routine() int begtime = clock();
#define end_routine()                                                          \
  int endtime = clock();                                                       \
  cerr << "\n\n"                                                               \
       << "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC      \
       << " ms\n\n";                                                           \
  return 0
#define ll long long int
#define ull unsigned long long int
#define db long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define pdd pair<db, db>
#define pll pair<ll, ll>
#define vpl vector<pll>
#define vll vector<ll>
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000007
#define eps 0.000001
#define stp fixed << setprecision(20)
#define endl '\n'

ll fact[3000005];

inline ll pow(ll x, ll y, ll p) {
  ll ans = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      ans = (ans * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return ans;
}
inline ll invmod(ll x, ll p) { return pow(x, p - 2, p); }

int main() {
  fastio;
#ifdef APNA_IO
  start_routine();
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif

  fact[0] = 1;
  for (ll i = 1; i < 3000005; i++) {
    fact[i] = (i * fact[i - 1]) % 1000003;
  }

  ll q;
  cin >> q;
  while (q--) {
    ll x, d, n;
    cin >> x >> d >> n;
    ll a = (x * invmod(d, 1000003)) % 1000003;

    ll l = a, r = (a + n - 1);
    if (l > r)
      swap(l, r);
    {
      ll x = fact[r];
      x = (x * invmod(fact[l - 1], 1000003)) % 1000003;
      x = (x * pow(d, n, 1000003)) % 1000003;
      cout << x << endl;
    }
  }

#ifdef APNA_IO
  end_routine();
#endif
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define start_routine() int begtime = clock();
#define end_routine()                                                          \
  int endtime = clock();                                                       \
  cerr << "\n\n"                                                               \
       << "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC      \
       << " ms\n\n";                                                           \
  return 0
#define ll long long int
#define ull unsigned long long int
#define db long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define pdd pair<db, db>
#define pll pair<ll, ll>
#define vpl vector<pll>
#define vll vector<ll>
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000007
#define eps 0.000001
#define stp fixed << setprecision(20)
#define endl '\n'

ll fact[3000005];

inline ll pow(ll x, ll y, ll p) {
  ll ans = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      ans = (ans * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return ans;
}
inline ll invmod(ll x, ll p) { return pow(x, p - 2, p); }

int main() {
  fastio;
#ifdef APNA_IO
  start_routine();
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif

  fact[0] = 1;
  for (ll i = 1; i < 3000005; i++) {
    fact[i] = (i * fact[i - 1]) % 1000003;
  }

  ll q;
  cin >> q;
  while (q--) {
    ll x, d, n;
    cin >> x >> d >> n;
    ll a = (x * invmod(d, 1000003)) % 1000003;

    if (x == 0) {
      cout << 0 << endl;
      continue;
    }

    if (d == 0) {
      cout << pow(x, n, 1000003) << endl;
      continue;
    }

    ll l = a, r = (a + n - 1) % 1000003;
    if (l + n - 1 >= 1000003 || l - 1 < 0)
      cout << 0 << endl;
    else {
      ll x = fact[r];
      x = (x * invmod(fact[l - 1], 1000003)) % 1000003;
      x = (x * pow(d, n, 1000003)) % 1000003;
      cout << x << endl;
    }
  }

#ifdef APNA_IO
  end_routine();
#endif
}