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)

{-# LANGUAGE
  ScopedTypeVariables, BangPatterns, TupleSections, ExplicitForAll,
  LambdaCase, MultiWayIf, Unsafe, RecordWildCards, FlexibleContexts, CPP,
  NoMonomorphismRestriction, GADTs, PatternGuards, MagicHash, UnboxedTuples,
  RankNTypes, EmptyDataDecls, EmptyCase, ViewPatterns #-}
{-# OPTIONS_GHC -O2 #-}


import Data.Bits
import Data.List
import Data.Maybe
import Data.Tuple
import Data.Ord
import Data.Int
import Data.Word
import Data.Char
import Data.Ratio
import Data.Function
import Data.STRef
import Data.IORef
import Data.Monoid
import Data.Functor
import System.IO
import Control.Arrow
import Control.Applicative
import Control.Monad
import Control.Monad.Primitive
import Control.Monad.State.Strict
import Control.Monad.ST
import Control.Monad.ST.Lazy (strictToLazyST, lazyToStrictST)
import qualified Control.Monad.ST.Lazy as STL
-- import Control.Monad.ST.Safe
import Control.DeepSeq
import Data.Coerce
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.ByteString.Builder as BSB
import Data.IntMap (IntMap)
import qualified Data.IntMap.Strict as IMS
import qualified Data.IntMap.Lazy as IML
import Data.IntSet (IntSet)
import qualified Data.IntSet as IS
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Storable.Mutable as VSM
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Primitive.Mutable as VPM
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Attoparsec.ByteString.Char8 as Atto
import GHC.Exts (build, Int(..), Int#,
                 (+#), (*#), (-#), (<#), (>=#), (==#), quotRemInt#,
                 remInt#, uncheckedIShiftRL#, andI#, orI#,
                isTrue#)

#define MOD 1000003
modulus :: Int
modulus = MOD

main :: IO ()
main = do
  q <- readInt <$> getLine
  queries <- VU.unfoldrN q (runStateT $ liftA3 (,,) rInt rInt rInt)
             <$> BSL.getContents
  BSB.hPutBuilder stdout
    $ VU.foldr (\x -> (BSB.intDec x <>) . (BSB.char7 '\n' <>)) mempty
    $ (`VU.map` queries)
    $ \(!x, !d, !n) -> query x d n

{-# INLINE query #-}
query :: Int -> Int -> Int -> Int
query (I# x#) (I# d#) (I# n#) = I# (query# x# d# n#)

{-# INLINE query# #-}
query# :: Int# -> Int# -> Int# -> Int#
query# x# d# n#
  | isTrue# (x# ==# 0#) = 0#
  | isTrue# (n# >=# MOD#) = 0#
  | isTrue# (d# ==# 0#) = x# `powRem#` n#
query# x# d# n#
  | isTrue# (end# >=# MOD#) = 0#
  | otherwise = (unI# $ factorial `VU.unsafeIndex` (I# end#))
                `mult#` (unI# $ invFact `VU.unsafeIndex` (I# begm1#))
                `mult#` (d# `powRem#` n#)
  where
    begm1# = (x# `mult#` invMod# d#) -# 1#
    end# = begm1# +# n#



{-# INLINE CONLIKE unI# #-}
unI# :: Int -> Int#
unI# = \(I# x#) -> x#

-- assumes 0 <= x# < modulus, y# >= 0#.
powRem# :: Int# -> Int# -> Int#
powRem# x# y# = go 1# x# y#
  where
    go acc# val# 0# = acc#
    go acc# val# expt#
      = go (if isTrue# (andI# expt# 1#) then acc# `mult#` val# else acc#)
           (val# `mult#` val#)
           (expt# `uncheckedIShiftRL#` 1#)

{-# NOINLINE factorial #-}
factorial :: VU.Vector Int
factorial = VU.scanl' mult 1 $ VU.tail $ VU.generate modulus id

{-# NOINLINE invFact #-}
invFact :: VU.Vector Int
invFact = VU.map invMod factorial

{-# INLINE mult #-}
mult :: Int -> Int -> Int
mult = \(I# x#) (I# y#) -> I# (mult# x# y#)

{-# INLINE mult# #-}
mult# :: Int# -> Int# -> Int#
mult# = \ x# y# -> (x# *# y#) `remInt#` MOD#

{-# INLINE invMod #-}
invMod :: Int -> Int
invMod = \(I# x#) -> I# (invMod# x#)

{-# INLINE invMod# #-}
-- assumes 0 < x < modulus.
invMod# :: Int# -> Int#
invMod# x# = go 0# MOD# 1# x#
  where
    go a# x# b# 0# = a# +# if isTrue# (a# <# 0#) then MOD# else 0#
    go a# x# b# y# = go b# y# (a# -# q# *# b#) z#
      where (# q#,z# #) = x# `quotRemInt#` y#

#define IL(f) {-# INLINE f #-}; f

IL(evalVals) :: [a] -> [a]
evalVals xs = build $ \c n -> foldr (c $!) n xs
IL(forceVals) :: (NFData a) => [a] -> [a]
forceVals xs = build $ \c n -> foldr (c $!!) n xs

rInt :: StateT BSL.ByteString Maybe Int
IL(rInt) = StateT $ BSL.readInt . BSL.dropWhile (<'!')
rIntS :: StateT BS.ByteString Maybe Int
IL(rIntS) = StateT $ BS.readInt . BS.dropWhile (<'!')

unlessM, whenM :: (Monad m) => m Bool -> m () -> m ()
IL(whenM) = (. flip when) . (>>=)
IL(unlessM) = (. flip unless) . (>>=)

IL(wrA) = A.writeArray
IL(rdA) = A.readArray
IL(mdA) = \arr f !i -> do
  ai <- rdA arr i
  let fai = f ai 
  wrA arr i fai
  return (ai,fai)
{-# INLINE mdA' #-}
mdA' = \arr f !i -> do
  !ai <- rdA arr i
  let !fai = f ai
  wrA arr i fai
  return (ai,fai)
IL(swapA) = \arr !i !j -> do
  ai <- rdA arr i
  wrA arr i =<< rdA arr j
  wrA arr j ai

#define D(f,r,d)\
  IL(f) :: Integral a=>a->d; f=fromIntegral;\
  IL(r) :: String->d; r=read
#define C(f,r,g,h,d) D(f,r,d);\
  g,h :: RealFrac a=>a->d; IL(g)=floor; IL(h)=ceiling
C(_toInteger_,readInteger,floorInteger,ceilInteger,Integer)
C(toInt,readInt,floorInt,ceilInt,Int)
C(toI8,readI8,floorI8,ceilI8,Int8)
C(toI16,readI16,floorI16,ceilI16,Int16)
C(toI32,readI32,floorI32,ceilI32,Int32)
C(toI64,readI64,floorI64,ceilI64,Int64)
C(toWord,readWord,floorWord,ceilWord,Word)
C(toW8,readW8,floorW8,ceilW8,Word8)
C(toW16,readW16,floorW16,ceilW16,Word16)
C(toW32,readW32,floorW32,ceilW32,Word32)
C(toW64,readW64,floorW64,ceilW64,Word64)
D(toDouble,readDouble,Double)
D(toFloat,readFloat,Float)
#undef D
#undef C

#define N(f,g,h,a,m)\
  IL(f) :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> e -> m (a i e);\
  f=A.newArray;\
  IL(g) :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> m (a i e);\
  g=A.newArray_;\
  IL(h) :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> [e] -> m (a i e);\
  h=A.newListArray
#define C(a,m)
N(newIOA,newIOA_,newIOAL,IOArray,IO)
N(newSTA,newSTA_,newSTAL,STArray s,ST s)
#undef C
#define C(a,m) MArray (a) e (m), 
N(newIOUA,newIOUA_,newIOUAL,IOUArray,IO)
N(newSTUA,newSTUA_,newSTUAL,STUArray s,ST s)
#undef C
#undef N

#undef IL