Problem:
Score : 800 points

Problem Statement

There are N squares arranged in a row, numbered 1 to N from left to right. Takahashi will stack building blocks on these squares, on which there are no blocks yet.

He wants to stack blocks on the squares evenly, so he will repeat the following operation until there are H blocks on every square:

Let M and m be the maximum and minimum numbers of blocks currently stacked on a square, respectively. Choose a square on which m blocks are stacked (if there are multiple such squares, choose any one of them), and add a positive number of blocks on that square so that there will be at least M and at most M + D blocks on that square.

Tell him how many ways there are to have H blocks on every square by repeating this operation. Since there can be extremely many ways, print the number modulo 10^9+7.

Constraints

2 \leq N \leq 10^6

1 \leq D \leq H \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N H D

Output

Print the number of ways to have H blocks on every square, modulo 10^9+7.

Sample Input 1

2 2 1

Sample Output 1

6

The possible transitions of (the number of blocks on Square 1, the number of blocks on Square 2) are as follows:

(0, 0) -> (0, 1) -> (1, 1) -> (1, 2) -> (2, 2)

(0, 0) -> (0, 1) -> (1, 1) -> (2, 1) -> (2, 2)

(0, 0) -> (0, 1) -> (2, 1) -> (2, 2)

(0, 0) -> (1, 0) -> (1, 1) -> (1, 2) -> (2, 2)

(0, 0) -> (1, 0) -> (1, 1) -> (2, 1) -> (2, 2)

(0, 0) -> (1, 0) -> (1, 2) -> (2, 2)

Thus, there are six ways to have two blocks on every square.

Sample Input 2

2 30 15

Sample Output 2

94182806

Sample Input 3

31415 9265 3589

Sample Output 3

312069529

Be sure to print the number modulo 10^9+7.

Solution in Rust (1.15.1) (judge verdict: Accepted)

// input
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

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

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

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

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

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

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

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

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

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

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

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

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

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

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

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

mod mint {
    use std::fmt;
    use std::ops::{
        Add, AddAssign, BitXor, BitXorAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign,
    };

    pub const MOD: i32 = 1_000_000_007;
    pub const ZERO: Mint = Mint { val: 0 };
    pub const ONE: Mint = Mint { val: 1 };
    pub const TWO: Mint = Mint { val: 2 };

    #[derive(Clone, Copy, Debug)]
    pub struct Mint {
        val: i32,
    }

    impl Add for Mint {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            let mut v = self;
            v += other;
            v
        }
    }

    impl AddAssign for Mint {
        fn add_assign(&mut self, other: Self) {
            self.val += other.val;
            if self.val >= MOD {
                self.val -= MOD;
            }
        }
    }

    impl Sub for Mint {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            let mut v = self;
            v -= other;
            v
        }
    }

    impl SubAssign for Mint {
        fn sub_assign(&mut self, other: Self) {
            self.val -= other.val;
            if self.val < 0 {
                self.val += MOD;
            }
        }
    }

    impl Mul for Mint {
        type Output = Self;
        fn mul(self, other: Self) -> Self {
            let mut v = self;
            v *= other;
            v
        }
    }

    impl MulAssign for Mint {
        fn mul_assign(&mut self, other: Self) {
            self.val = ((self.val as i64) * (other.val as i64) % (MOD as i64)) as i32;
        }
    }

    impl Div for Mint {
        type Output = Self;
        fn div(self, other: Self) -> Self {
            let mut v = self;
            v /= other;
            v
        }
    }

    impl DivAssign for Mint {
        fn div_assign(&mut self, other: Self) {
            *self *= other ^ Mint::from(MOD - 2);
        }
    }

    impl BitXor for Mint {
        type Output = Self;
        fn bitxor(mut self, other: Self) -> Self {
            self ^= other;
            self
        }
    }

    impl BitXorAssign for Mint {
        fn bitxor_assign(&mut self, mut other: Self) {
            let mut x = *self;
            *self = 1.into();
            while other.val != 0 {
                if (other.val & 1) == 1 {
                    *self *= x;
                }
                x *= x;
                other.val >>= 1;
            }
        }
    }

    // i32
    impl Add<i32> for Mint {
        type Output = Self;
        fn add(self, other: i32) -> Self {
            self + Mint::from(other)
        }
    }

    impl AddAssign<i32> for Mint {
        fn add_assign(&mut self, other: i32) {
            *self += Mint::from(other);
        }
    }

    impl Sub<i32> for Mint {
        type Output = Self;
        fn sub(self, other: i32) -> Self {
            self - Mint::from(other)
        }
    }

    impl SubAssign<i32> for Mint {
        fn sub_assign(&mut self, other: i32) {
            *self -= Mint::from(other);
        }
    }

    impl Mul<i32> for Mint {
        type Output = Self;
        fn mul(self, other: i32) -> Self {
            self * Mint::from(other)
        }
    }

    impl MulAssign<i32> for Mint {
        fn mul_assign(&mut self, other: i32) {
            *self *= Mint::from(other);
        }
    }

    impl Div<i32> for Mint {
        type Output = Self;
        fn div(self, other: i32) -> Self {
            self / Mint::from(other)
        }
    }

    impl DivAssign<i32> for Mint {
        fn div_assign(&mut self, other: i32) {
            *self /= Mint::from(other);
        }
    }

    impl BitXor<i32> for Mint {
        type Output = Self;
        fn bitxor(self, other: i32) -> Self {
            self ^ Mint::from(other)
        }
    }

    impl BitXorAssign<i32> for Mint {
        fn bitxor_assign(&mut self, other: i32) {
            *self ^= Mint::from(other);
        }
    }

    impl From<i32> for Mint {
        fn from(mut item: i32) -> Self {
            while item >= MOD {
                item -= MOD
            }
            while item < 0 {
                item += MOD
            }
            Mint { val: item }
        }
    }

    // usize
    impl Add<usize> for Mint {
        type Output = Self;
        fn add(self, other: usize) -> Self {
            self + Mint::from(other)
        }
    }

    impl AddAssign<usize> for Mint {
        fn add_assign(&mut self, other: usize) {
            *self += Mint::from(other);
        }
    }

    impl Sub<usize> for Mint {
        type Output = Self;
        fn sub(self, other: usize) -> Self {
            self - Mint::from(other)
        }
    }

    impl SubAssign<usize> for Mint {
        fn sub_assign(&mut self, other: usize) {
            *self -= Mint::from(other);
        }
    }

    impl Mul<usize> for Mint {
        type Output = Self;
        fn mul(self, other: usize) -> Self {
            self * Mint::from(other)
        }
    }

    impl MulAssign<usize> for Mint {
        fn mul_assign(&mut self, other: usize) {
            *self *= Mint::from(other);
        }
    }

    impl Div<usize> for Mint {
        type Output = Self;
        fn div(self, other: usize) -> Self {
            self / Mint::from(other)
        }
    }

    impl DivAssign<usize> for Mint {
        fn div_assign(&mut self, other: usize) {
            *self /= Mint::from(other);
        }
    }

    impl BitXor<usize> for Mint {
        type Output = Self;
        fn bitxor(self, other: usize) -> Self {
            self ^ Mint::from(other)
        }
    }

    impl BitXorAssign<usize> for Mint {
        fn bitxor_assign(&mut self, other: usize) {
            *self ^= Mint::from(other);
        }
    }

    impl From<usize> for Mint {
        fn from(item: usize) -> Self {
            Mint::from((item % (MOD as usize)) as i32)
        }
    }

    impl fmt::Display for Mint {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "{}", self.val)
        }
    }
}

fn main() {
    input! {
        n: usize,
        h: usize,
        d: usize,
    }

    let mut f = vec![mint::ONE; n + 1];
    let mut sumf = vec![mint::ZERO; n + 1];
    for i in 0..n {
        f[i + 1] = f[i] * mint::Mint::from(i + 1);
    }
    for i in 0..n {
        sumf[i + 1] = sumf[i] + f[i + 1];
    }

    let mut dp = vec![mint::ZERO; h + 2];
    dp[1] += mint::ONE;
    dp[d + 1] -= mint::ONE;
    for i in 1..h {
        let l = i + 1;
        let u = std::cmp::min(h, i + d) + 1;

        let v = dp[i];
        dp[l] += v * sumf[n] + v;
        dp[u] -= v * sumf[n];
    }
    // eprintln!("{:?}", f);
    // eprintln!("{:?}", sumf);
    // eprintln!("{:?}", dp);
    println!("{}", dp[h] * f[n]);
}