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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using E = System.Linq.Enumerable;

partial class Solver
{
    const int Mod = 1000003;
    public void Run()
    {
        var Q = ni();

        var MulTable = new long[Mod + 120];
        MulTable[1] = 1;
        for (int i = 2; i < MulTable.Length; i++)
        {
            MulTable[i] = (MulTable[i - 1] * i) % Mod;
        }

        for (int _ = 0; _ < Q; _++)
        {
            var x = nl();
            var d = nl();
            var n = nl();
            long ans = 0;

            if (d == 0)
            {
                ans = ModPow2(x, n, Mod);
            }
            else
            {
                // a = x / d
                var a = x * Inverse(d, Mod) % Mod;
                var dn = ModPow2(d, n, Mod);
                // a * (a+1) * ... * (a+n-1)

                if (a + n - 1 >= Mod)
                {
                    ans = 0;
                }
                else
                {
                    ans = MulTable[a + n - 1];
                    if (a - 1 > 0)
                    {
                        ans = (ans * Inverse(MulTable[a - 1], Mod)) % Mod;
                    }
                }

                ans *= dn;
                ans %= Mod;
            }

            cout.WriteLine(ans);
        }
    }

    static public long ModPow2(long x, long n, long mod)
    {
        long t = x, ret = 1;
        while (n > 0)
        {
            if ((n & 1) != 0)
            {
                ret = (t * ret) % mod;
            }
            t = t * t % mod;
            n >>= 1;
        }
        return ret;
    }
    static public long ExtendedGcd(long a, long b, ref long x, ref long y)
    {
        if (b == 0)
        {
            x = 1; y = 0;
            return a;
        }
        else
        {
            long d = ExtendedGcd(b, a % b, ref y, ref x);
            y -= a / b * x;
            return d;
        }
    }

    static public long Inverse(long a, long mod)
    {
        long x = 0, y = 0;
        if (ExtendedGcd(a, mod, ref x, ref y) == 1)
            return (x + mod) % mod;
        else
            return -1;
    }
}

// PREWRITEN CODE BEGINS FROM HERE
partial class Solver : Scanner
{
    public static void Main(string[] args)
    {
#if LOCAL
        new Solver(Console.In, Console.Out).Run();
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver(Console.In, Console.Out).Run();
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader)
    {
        this.cin = reader;
        this.cout = writer;
    }
    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer)
    {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

public class Scanner
{
    private readonly TextReader Reader;
    private readonly Queue<string> TokenQueue = new Queue<string>();
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    public Scanner()
        : this(Console.In)
    {
    }

    public Scanner(TextReader reader)
    {
        this.Reader = reader;
    }

    public int NextInt() { return int.Parse(Next(), ci); }
    public long NextLong() { return long.Parse(Next(), ci); }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size)
    {
        var array = new string[size];
        for (int i = 0; i < size; i++) array[i] = Next();
        return array;
    }
    public int[] NextIntArray(int size)
    {
        var array = new int[size];
        for (int i = 0; i < size; i++) array[i] = NextInt();
        return array;
    }

    public long[] NextLongArray(int size)
    {
        var array = new long[size];
        for (int i = 0; i < size; i++) array[i] = NextLong();
        return array;
    }

    public double[] NextDoubleArray(int size)
    {
        var array = new double[size];
        for (int i = 0; i < size; i++) array[i] = NextDouble();
        return array;
    }

    public string Next()
    {
        if (TokenQueue.Count == 0)
        {
            if (!StockTokens()) throw new InvalidOperationException();
        }
        return TokenQueue.Dequeue();
    }

    public bool HasNext()
    {
        if (TokenQueue.Count > 0)
            return true;
        return StockTokens();
    }
    static readonly char[] _separator = new[] { ' ' };
    private bool StockTokens()
    {
        while (true)
        {
            var line = Reader.ReadLine();
            if (line == null) return false;
            var tokens = line.Split(_separator, StringSplitOptions.RemoveEmptyEntries);
            if (tokens.Length == 0) continue;
            foreach (var token in tokens)
                TokenQueue.Enqueue(token);
            return true;
        }
    }
}