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 Haskell (GHC 7.10.3) (judge verdict: Wrong Answer)

-- https://github.com/minoki/my-atcoder-solutions
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
import Data.Char
import Data.Int
import Data.List
import Control.Monad
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM
import qualified Data.ByteString.Char8 as BS
import Data.Monoid

exEuclid :: (Eq a, Integral a) => a -> a -> (a, a, a)
exEuclid !f !g = loop 1 0 0 1 f g
  where loop !u0 !u1 !v0 !v1 !f 0 = (f, u0, v0)
        loop !u0 !u1 !v0 !v1 !f g =
          case divMod f g of
            (q,r) -> loop u1 (u0 - q * u1) v1 (v0 - q * v1) g r

modulo = 10^6+3 :: Int64
addMod, subMod, mulMod, divM :: Int64 -> Int64 -> Int64
addMod !x !y = (x + y) `rem` modulo
subMod !x !y = (x - y) `mod` modulo
mulMod !x !y = (x * y) `rem` modulo
recipM :: Int64 -> Int64
recipM !x = case exEuclid x modulo of
             (1,a,_) -> a `mod` modulo
             (-1,a,_) -> (-a) `mod` modulo
divM !x !y = x `mulMod` recipM y

newtype N = N { unwrapN :: Int64 } deriving (Eq)
instance Show N where
  show (N x) = show x
instance Num N where
  N x + N y = N ((x + y) `rem` modulo)
  N x - N y = N ((x - y) `mod` modulo)
  N x * N y = N ((x * y) `rem` modulo)
  fromInteger n = N (fromInteger (n `mod` fromIntegral modulo))
  abs = undefined; signum = undefined

instance Fractional N where
  N x / N y = N (divM x y)
  recip (N x) = N (recipM x)
  fromRational = undefined

factL :: [N]
factL = 1 : zipWith (*) factL (map fromInteger [1..])
factFrom :: Integer -> N -> [N]
factFrom n v0 = v0 : zipWith (*) factL (map fromInteger [n+1..])
factV :: V.Vector N
factV = V.fromListN 200000 (take 200000 factL)
  <> V.fromListN (500000 - 200000) (take (500000 - 200000) $ factFrom 200000 177247)
  <> V.fromListN (700000 - 500000) (take (700000 - 500000) $ factFrom 500000 2)
  <> V.fromListN (total - 700000) (take (fromIntegral modulo - 700000) $ factFrom 700000 426941)
  where total = fromIntegral modulo :: Int
factM :: Int64 -> N
factM n | n < modulo = factV V.! fromIntegral n
        | otherwise = 0

solve :: Int -> Int -> Int -> N
solve x 0 n = (fromIntegral x)^n
solve !x !d !n = let !x' = fromIntegral x :: N
                     !d' = fromIntegral d :: N
                     !xd = unwrapN (x' / d')
                 in if xd + fromIntegral n - 1 >= modulo
                    then 0
                    else d'^n * factM (xd + fromIntegral n - 1) / factM (xd - 1)

main = do
  q :: Int <- readLn
  replicateM_ q $ do
    [x,d,n] <- unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
    print (solve x d n)