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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

using ll = int_fast64_t;
constexpr ll llmax = INT_FAST64_MAX; // std::numeric_limits<int_fast64_t>::max()
constexpr ll llmin = INT_FAST64_MIN; // std::numeric_limits<int_fast64_t>::min()
using ull = uint_fast64_t;
constexpr ull ullmax = UINT_FAST64_MAX; // std::numeric_limits<uint_fast64_t>::max()

#define FOR(i, b, e) for (int i = (b); i < (e); ++i)
#define RFOR(i, b, e) for (int i = ((e) - 1); (b) <= i; --i)
#define REP(i, N) for (int i = 0; i < (N); ++i)
#define RREP(i, N) for (int i = ((N) - 1); 0 <= i; --i)
#define scanInt(x) int x; scanf("%d", &(x)); getchar();
#define scanLLInt(x) ll x; scanf("%lld", &(x)); getchar();
#define scanVecInt(A, N) vector<int> A(N); REP(i, N){scanf("%d", &(A[i])); getchar();}
#define scanVecLLInt(A, N) vector<ll> A(N); REP(i, N){scanf("%lld", &(A[i])); getchar();}

constexpr uint mod = 1000003;

// modをとった和を返す
inline
uint modPlus(uint a, uint b){return (a + b) % mod;}

// modをとった差を返す
inline
uint modMinus(uint a, uint b){return (mod + a - b) % mod;}

// modをとった積を返す
inline
uint modMultiple(uint a, uint b){return (static_cast<long long>(a) * b) % mod;}

// 階乗数n!を最初に計算して、呼ばれた時に返す
class Factorial {
    std::vector<uint> table;
public:
    Factorial(uint max) : table(max) // max:テーブルのサイズ
    {
        if (max == 0) return;
        table[0] = 1;
        for (uint i = 1; i < max; ++i){
            table[i] = modMultiple(table[i - 1], i + 1);
        }
    }

    uint operator () (int n)
    {
        if (n < 0) return 0;
        if (n == 0) return 1;
        return table[n - 1];
    }
};

// 階上数n!の逆数（逆元）を最初に計算して、呼ばれた時に返す
class FactorialInverse {
    std::vector<uint> table;
    Factorial& fact;
public:
    FactorialInverse(uint max, Factorial fact) : table(max), fact(fact) // max:テーブルのサイズ
    {
        if (max == 0) return;
        uint digit  = 0;
        {
            uint temp = mod - 2;
            while (temp > 0){
                temp >>= 1;
                ++digit;
            }
        }
        uint origin = fact(max);
        std::vector<uint> powerorigin2(digit);
        powerorigin2[0] = origin;
        for (uint i = 1; i < digit; ++i){
            powerorigin2[i] = modMultiple(powerorigin2[i - 1], powerorigin2[i - 1]);
        }
        {
            uint table_max = 1;
            uint temp = mod - 2;
            for (uint i = 0; temp > 0; ++i, temp >>= 1){
                if (!(temp & 1)) continue;
                table_max = modMultiple(table_max, powerorigin2[i]);
            }
            table[max - 1] = table_max; // origin^(mod - 2)
        }
        for (int i = max - 2; i >= 0; --i){
            table[i] = modMultiple(table[i + 1], i + 2);
        }
    }

    uint operator () (int n)
    {
        if (n < 0) return 0;
        if (n == 0) return 1;
        return table[n - 1];
    }
};

// max以下の全ての数nの逆数（逆元）n^(mod - 2)を最初に計算して、呼ばれた時に返す
class ModInverse {
    std::vector<uint> table;
public:
    ModInverse(uint max) // max: 使いたい引数nの最大
        : table(max)
    {
        for (int n = 1; n <= max; ++n){
            uint mod_minus_2 = mod - 2;
            vector<uint> temp_table(static_cast<uint>(floor(log2f(mod_minus_2))) + 1);
            temp_table[0] = n;
            for (uint i = 1; i < temp_table.size(); ++i){
                temp_table[i] = modMultiple(temp_table[i - 1], temp_table[i - 1]); // n ^ (2 ^ i)
            }
            table[n - 1] = 1;
            for (uint i = 0; mod_minus_2; ++i, mod_minus_2 >>= 1){
                if (!(mod_minus_2 & 1)) continue;
                table[n - 1] = modMultiple(table[n - 1], temp_table[i]);
            }
        }
    }
    uint operator () (uint n)
    {
        return table[n - 1];
    }
};

// N^(2 ^ i)を最初に計算して、呼ばれた時にN^xを計算して返す
class PowerN {
    std::vector<uint> table;
public:
    PowerN(uint N, uint max) // max:使いたい引数nの最大
    {
        if (max == 0) return;
        table.resize(static_cast<uint>(floor(log2f(max))) + 1);
        table[0] = N;
        for (uint i = 1; i < table.size(); ++i){
            table[i] = modMultiple(table[i - 1], table[i - 1]); // N ^ (2 ^ i)
        }
    }

    uint operator () (int n)
    {
        if (n < 0) return 0;
        if (n == 0) return 1;
        uint result = 1;
        for (uint i = 0; n; ++i, n >>= 1){
            if (!(n & 1)) continue;
            result = modMultiple(result, table[i]);
        }
        return result;
    }
};

int main()
{
    Factorial fact(mod - 1);
    FactorialInverse factinv(mod - 1, fact);
    ModInverse modinv(mod - 1);

    scanInt(Q);
    REP(q, Q){
        scanInt(xq);
        scanInt(dq);
        scanInt(nq);

        if (dq == 0){
            PowerN powx(xq, nq);
            cout << powx(nq) << endl;
        }else{
            PowerN powd(dq, nq);
            const int xpdm1 = modMinus(modMultiple(xq, modinv(dq)), 1);
            cout << modMultiple(powd(nq), modMultiple(fact(modPlus(xpdm1, nq)), factinv(xpdm1)));
            cout << endl;
        }
    }
}