Problem:
Score : 600 points

Problem Statement

Consider the following arithmetic progression with n terms:

x, x + d, x + 2d, \ldots, x + (n-1)d

What is the product of all terms in this sequence?
Compute the answer modulo 1\ 000\ 003.

You are given Q queries of this form.
In the i-th query, compute the answer in case x = x_i, d = d_i, n = n_i.

Constraints

1 \leq Q \leq 10^5

0 \leq x_i, d_i \leq 1\ 000\ 002

1 \leq n_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
x_1 d_1 n_1
:
x_Q d_Q n_Q

Output

Print Q lines.

In the i-th line, print the answer for the i-th query.

Sample Input 1

2
7 2 4
12345 67890 2019

Sample Output 1

9009
916936

For the first query, the answer is 7 \times 9 \times 11 \times 13 = 9009.
Don't forget to compute the answer modulo 1\ 000\ 003.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.util.*;

public class Main {
  public static long MOD = (long)Math.pow(10, 6) + 3;
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long[] kai = new long[(int)MOD];
    long[] inkai = new long[(int)MOD];
    long[] in = new long[(int)MOD];
    kai[0] = 1;
    kai[1] = 1;
    inkai[0] = 1;
    inkai[1] = 1;
    in[1] = 1;
    for(long i = 2; i < MOD; i++) {
      kai[(int)i] = (i * kai[(int)i - 1]) % MOD;
      inkai[(int)i] = func(kai[(int)i], MOD - 2);
      in[(int)i] = func(i, MOD - 2);
    }
    int q = sc.nextInt();
    long[] ans = new long[q];
    for(int i = 0; i < q; i++) {
      long x = sc.nextLong();
      long d = sc.nextLong();
      long n = sc.nextLong();
   //   long ans = 0;
      if(d == 0) {
        if(x == 0) {
          ans[i] = 0;
        } else {
          ans[i] = func(x, n % (MOD - 1));
        }
      } else {
        x = (x * in[(int)d]) % MOD;
        if(x == 0) {
          ans[i] = 0;
        } else {
          if((int)(x + n - 1) >= MOD) {
            ans[i] = 0;
          } else {
            long t = func(d, n % (MOD - 1));
            ans[i] = (((kai[(int)(x + n - 1)] * inkai[(int)(x - 1)]) % MOD) * t) % MOD;
          }
        }
      }
    }

    for(int i = 0; i < q; i++) {
      System.out.println(ans[i]);
    }
  }

  public static long func(long a, long x) {
    if(x == 0) return 1;
    if(x >= 1) {
      if(x % 2 == 0) {
        long t = func(a, x / 2);
        return (t * t) % MOD;
      } else {
        long t = func(a, x - 1);
        return (a * t) % MOD;
      }
    }
    return 0;
  } 
}