open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module HourRank21 =
    type cacheMap<'a, 'b> = System.Collections.Generic.Dictionary<'a, 'b>

    let memoizeMap (dict:cacheMap<_, _>) f =
        fun n ->
            match dict.TryGetValue n with
            | (true, v) -> v
            | _ ->
                let temp = f(n)
                dict.Add(n, temp)
                temp

    let memoize f = memoizeMap (new cacheMap<_, _> ()) f

    (* Returns r = x % p where 0<=r<p *)
    let inMod p x = let r = x % p in if r < 0L then r + p else r

    let rec extendedGcdInt64 x y =
        let c, z = y / x, y % x
        if z = 0L then 1L, 0L, x
        else
            let a, b, r = extendedGcdInt64 z x
            b - a * c, a, r

    (* Returns (i, r) where r is the GCD of p and x, and
            x * i % p = r *)
    let getInverseInt64 p x =
        let a, _, r = extendedGcdInt64 x p
        inMod p a, r

    [<AbstractClass>]
    type Arithmetic<'a when 'a:comparison> () =
        abstract member ``0``: 'a
        abstract member ``1``: 'a
        abstract member Add: 'a -> 'a -> 'a
        abstract member Mul: 'a -> 'a -> 'a
        abstract member Div: 'a -> 'a -> 'a
        abstract member Nega: 'a -> 'a
        abstract member IsZero: 'a -> bool

        member x.Sub a b = x.Add a (x.Nega b)

    [<AbstractClass>]
    type Integer<'a when 'a:comparison> () =
        inherit Arithmetic<'a> ()
        abstract member Mod: 'a -> 'a -> 'a
        abstract member Gcd: 'a -> 'a -> 'a

    type Int64ModHelper (p) =
        inherit Integer<int64> ()

        let inv = memoize (fun d -> getInverseInt64 p d |> fst)

        override x.``0`` = 0L
        override x.``1`` = 1L
        override x.Add a b = (a + b) % p
        override x.Mul a b = a * b % p
        override x.Div a b = inv b |> x.Mul a
        override x.Nega a = (-a % p + p) % p
        override x.IsZero a = a % p = 0L

        override x.Mod a b = failwith "not implemented"
        override x.Gcd a b = failwith "not implemented"

    (* Cal x^t. Function f is the multiple operator, and u is unit of f. *)
    let quickExp x t u f =
        let rec inner r b d =
            if d = 0L then r
            else
                let nr = if d % 2L = 1L then f r b else r
                inner nr (f b b) (d / 2L)
        inner u x t

    type MatrixOperator<'a when 'a: comparison> (num: Arithmetic<'a>) =
        member x.Add a b =
            let n, m = Array2D.length1 a, Array2D.length2 a
            if n <> Array2D.length1 b || m <> Array2D.length2 b then
                invalidArg "b" "size of the second matrix should match that of the first one"
            Array2D.init n m (fun i j -> num.Add a.[i, j] b.[i, j])

        member x.Mul a b =
            let n, k = Array2D.length1 a, Array2D.length2 a
            if k <> Array2D.length1 b then
                invalidArg "b" "row number of the second matrix should match column number of the first one"
            let m = Array2D.length2 b
            let f i j r c = num.Mul a.[i, c] b.[c, j] |> num.Add r
            Array2D.init n m (fun i j -> [0..k-1] |> Seq.fold (f i j) num.``0``)

        member x.Exp a m =
            let n = Array2D.length1 a
            if Array2D.length2 a <> n then
                invalidArg "a" "input matrix of exp should be n*n"
            let I = Array2D.init n n (fun i j -> if i = j then num.``1`` else num.``0``)
            quickExp a m I x.Mul

    let ``Sam's Numbers`` () =
        let Mod = 1000000009L
        let s = ReadNext () |> int64
        let m, d = Read2Int ()

        let gx = memoize (fun y ->
            [1..m]
            |> Seq.where (fun x -> y <= x + d && x <= y + d)
            |> Seq.cache)
        let sumInMod s f = let x = s |> Seq.sumBy f in x % Mod

        let fb =
            let a = Array2D.zeroCreate (m + 1) (m + 1)
            for i in [1..m] do
                for y in [1..i-1] do
                    a.[i, y] <- sumInMod (gx y) (fun x -> a.[i - y, x])
                a.[i, i] <- 1L
            a

        if s <= int64 m then
            sumInMod [1..m] (fun y -> fb.[int s, y])
        else
            let gs i x = i * m + x - 1
            let um = (m + 1 ) * m
            let arr =
                let a = Array2D.zeroCreate um um
                let toMp1 = [1..m] |> Seq.collect (fun y -> gx y |> Seq.map (fun x -> gs 0 y, gs (y - 1) x))
                let toCM = Seq.allPairs [1..m] [1..m] |> Seq.map (fun (j, y) -> gs j y, gs (j - 1) y)
                [toMp1; toCM] |> Seq.concat |> Seq.iter (fun (i, j) -> a.[i, j] <- 1L)
                a
            let matOp = new MatrixOperator<_> (new Int64ModHelper (Mod))
            let r = matOp.Exp arr (s - int64 m)
            sumInMod [1..m] (fun i -> [1..i] |> Seq.sumBy (fun x ->
                let cs, v = gs (m - i) x, fb.[i, x]
                sumInMod [1..m] (fun y -> r.[gs 0 y, cs] * v)))
        |> printfn "%d"

    let Entry = ``Sam's Numbers``

HourRank21.Entry ()open System
open System.Collections.Generic

module InputUtility =
    let InputTokens = new Queue<string>()

    let EnsureInput () =
        if InputTokens.Count = 0 then
            let input = Console.ReadLine ()
            let s = input.Split ([|' '|], StringSplitOptions.RemoveEmptyEntries)
            if s.Length > 0 then
                for x in s do InputTokens.Enqueue(x)
                true
            else false
        else true

    let rec ReadNext () =
        if EnsureInput () then InputTokens.Dequeue ()
        else ReadNext ()

    let ReadNextInt () = ReadNext () |> int

    let Read2Int () = ReadNextInt (), ReadNextInt ()

    let ReadListOf f n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop ((ReadNext () |> f) :: acc) (c-1)
        loop [] n

    let MultipleCaseWithId f =
        let caseNum = ReadNextInt ()
        let rec loop index =
            if index = caseNum then ()
            else
                f index
                loop (index + 1)
        loop 0

    let MultipleCase f = MultipleCaseWithId (fun _ -> f ())

    let ReadList n =
        let rec loop acc = function
            | 0 -> List.rev acc
            | c -> loop (ReadNext () :: acc) (c-1)
        loop [] n

open InputUtility

module ARC075 =
    let private Bugged () = 
        let n = ReadNextInt ()
        let rec cal i (f: _ array) (g: _ array) =
            if i = n then
                f |> Seq.skip 1 |> Seq.max
            else
                [0..9] |> Seq.iter (fun i -> g.[i] <- f.[i])
                let d = ReadNextInt ()
                for i in [0..9] do
                    if f.[i] > -1 then
                        let j = (i + d) % 10
                        g.[j] <- max g.[j] (f.[i] + d)
                cal (i + 1) g f
        let na () = Array.create 10 -1
        let f = na ()
        f.[0] <- 0
        cal 0 f (na ())
        |> max 0
        |> printfn "%d"

    let Entry = Bugged

ARC075.Entry ()