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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

#![allow(unused_imports)]

use std::cell::RefCell;
use std::cmp::{max, min, Ordering};
use std::collections::*;
use std::fmt::{Debug, Formatter, Write as FmtWrite};
use std::io::{stderr, stdin, BufRead, Write};
use std::mem::{replace, swap};
use std::ops::*;
use std::rc::Rc;

// -----------------------------------------------
// Framework <https://github.com/vain0x/procon>
// -----------------------------------------------

//
// Macro debug!
//

#[cfg(debug_assertions)]
include!{"./procon/debug.rs"}

#[cfg(not(debug_assertions))]
macro_rules! debug {
    ($($arg:expr),*) => {};
}

//
// Macro input!
//

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

//
// IteratorExt
//

trait IteratorExt: Iterator + Sized {
    fn vec(self) -> Vec<Self::Item> {
        self.collect()
    }
}

impl<T: Iterator> IteratorExt for T {}


fn expmod(x: u64, e: u64, m: u64) -> u64 {
    let mut x = x;
    let mut e = e;

    if e == 0 {
        1
    } else if e % 2 == 0 {
        let y = (x * x) % m;
        expmod(y, e/2, m)
    } else {
        let y = (x * x) % m;
        x * expmod(y, e/2, m) % m
    }
}


fn product(x: u64, d: u64, n: u64, m: u64, exponent_to_value: &Vec<u64>, value_to_exponent: &Vec<u64>) -> u64 {
    let e = (0..n).fold(0, |a, i| {
        let y = (x + i * d) % m;
        a + value_to_exponent[y as usize]
    });
    let e = e % (m - 1);
    exponent_to_value[e as usize]
}


fn main() {
    // input! {
    //     n: isize,
    //     xs: [isize; n],
    // }

    const M: u64 = 1_000_003;
    const base: u64 = 2;
    let mut exponent_to_value: Vec<u64> = Vec::with_capacity(M as usize);
    let mut value_to_exponent: Vec<u64> = vec![0; M as usize];

    for e in 0..M {
        let x = expmod(base, e, M);
        exponent_to_value.push(x);
        value_to_exponent[x as usize] = e;
    }

    // Check that the base is a primitive root.
    assert!(value_to_exponent[2..].iter().all(|x| *x != 0));

    // let y = product(7, 2, 4, M, &exponent_to_value, &value_to_exponent);
    // debug!(y);

    input! {
        q: usize,
        xs: [(u64, u64, u64); q],
    }

    let ys = xs.into_iter().map(|(x, d, n)| product(x, d, n, M, &exponent_to_value, &value_to_exponent)).vec();

    for y in ys {
        println!("{}", y);
    }

}