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 C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;

public static class P
{
    public static void Main()
    {
        var nhd = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var n = nhd[0];
        var h = nhd[1];
        var d = nhd[2];
        //最大値の遷移を考える。
        //最大値の遷移を考えた時に、それぞれのノードについて1個-n個の可能性がある。
        //最大値がn回現れる場合、次にそれが取られるときの通り数はn!
        //なので、sum_1^n i! が掛けられる
        //経路長がlの場合、accumFactorial^(l-1)*n! 通り余計な分がある(最初のn個0のぶん)
        //あとは多分経路長がl個のものが何個、みたいなのが求められれば嬉しい
        //l回DP回した時のaの数、がそれに該当しそう 絶対ヤバい
        //(1+x^2+x^3...x^d)^nのx^nの項が該当する
        //l個に分割する(要素d個まで) とも言いかえられる これを高速に求めたい
        //要素D個より多くある要素が一つある→-dしてあげればいい
        //((n-1)!/((n-l)!*(l-1)!) - n*((n-1-d)!/((n-l-d)!*(l-1)!) + (n-1)*((n-1-2d)!/((n-l-2d)!*(l-1)!) -
        //ってなる l+1した時は、((n-d-l-1)!/(n-d-l)! == n-d-l) * ((l-1)!/(l-1-1)! == l-1) == (n-d-l)*(l-1)で割る
        //変化する量dが入ってるからいける

        //掛ける数は経路長に依存しない形で分解できるので、実際は通過する場合に掛ける、で良いのでは?
        ModInt factorialAccum = 0;
        ModInt factorial = 1;
        for (int i = 1; i <= n; i++)
            factorialAccum += factorial *= i;
        SegmentTree<ModInt1e9p6> segTree = new SegmentTree<ModInt1e9p6>(h + 1, 0, (x, y) => x + y);
        segTree.Operate(0, 1);
        for (int i = 0; i <= h; i++)
        {
            segTree.Operate(i + 1, Min(i + d, segTree.Size - 1), segTree[i]);
        }
        ModInt res = 1;
        for (int i = 0; i < h - 1; i++)
        {
            res *= Power(factorialAccum, segTree[i] * segTree[n - i]);
        }
        Console.WriteLine(res * factorial);
    }

    static ModInt Power(ModInt n, long m)
    {
        ModInt pow = n;
        ModInt res = 1;
        while (m > 0)
        {
            if ((m & 1) == 1) res *= pow;
            pow *= pow;
            m >>= 1;
        }
        return res;
    }
}


class SegmentTree<T>
{
    public readonly int Size;
    T[] Operators;
    Func<T, T, T> Merge;
    int LeafCount;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public SegmentTree(int size, T identity, Func<T, T, T> merge)
    {
        Size = size;
        Merge = merge;
        LeafCount = 1;
        while (LeafCount < size) LeafCount <<= 1;
        Operators = new T[LeafCount << 1];
        for (int i = 0; i < Operators.Length; i++)
            Operators[i] = identity;
    }
    public T this[int index]
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get { return Query(index); }
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Operate(int i, T x) { i += LeafCount; Operators[i] = Merge(Operators[i], x); }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Operate(int l, int r, T x)
    {
        for (l += LeafCount, r += LeafCount; l <= r; l = (l + 1) >> 1, r = (r - 1) >> 1)
        {
            if ((l & 1) == 1) Operators[l] = Merge(Operators[l], x);
            if ((r & 1) == 0) Operators[r] = Merge(Operators[r], x);
        }
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public T Query(int i)
    {
        i += LeafCount;
        T res = Operators[i];
        while (0 < (i >>= 1)) res = Merge(res, Operators[i]);
        return res;
    }
}


struct ModInt
{
    public const int Mod = 1000000007;
    const long POSITIVIZER = ((long)Mod) << 31;
    long Data;
    public ModInt(long data) { if ((Data = data % Mod) < 0) Data += Mod; }
    public static implicit operator long(ModInt modInt) => modInt.Data;
    public static implicit operator ModInt(long val) => new ModInt(val);
    public static ModInt operator +(ModInt a, int b) => new ModInt() { Data = (a.Data + b + POSITIVIZER) % Mod };
    public static ModInt operator +(ModInt a, long b) => new ModInt(a.Data + b);
    public static ModInt operator +(ModInt a, ModInt b) { long res = a.Data + b.Data; return new ModInt() { Data = res >= Mod ? res - Mod : res }; }
    public static ModInt operator -(ModInt a, int b) => new ModInt() { Data = (a.Data - b + POSITIVIZER) % Mod };
    public static ModInt operator -(ModInt a, long b) => new ModInt(a.Data - b);
    public static ModInt operator -(ModInt a, ModInt b) { long res = a.Data - b.Data; return new ModInt() { Data = res < 0 ? res + Mod : res }; }
    public static ModInt operator *(ModInt a, int b) => new ModInt(a.Data * b);
    public static ModInt operator *(ModInt a, long b) => a * new ModInt(b);
    public static ModInt operator *(ModInt a, ModInt b) => new ModInt() { Data = a.Data * b.Data % Mod };
    public static ModInt operator /(ModInt a, ModInt b) => new ModInt() { Data = a.Data * GetInverse(b) % Mod };
    public static bool operator ==(ModInt a, ModInt b) => a.Data == b.Data;
    public static bool operator !=(ModInt a, ModInt b) => a.Data != b.Data;
    public override string ToString() => Data.ToString();
    public override bool Equals(object obj) => (ModInt)obj == this;
    public override int GetHashCode() => (int)Data;
    static long GetInverse(long a)
    {
        long div, p = Mod, x1 = 1, y1 = 0, x2 = 0, y2 = 1;
        while (true)
        {
            if (p == 1) return x2 + Mod; div = a / p; x1 -= x2 * div; y1 -= y2 * div; a %= p;
            if (a == 1) return x1 + Mod; div = p / a; x2 -= x1 * div; y2 -= y1 * div; p %= a;
        }
    }
}


struct ModInt1e9p6
{
    public const int Mod = 1000000007;
    const long POSITIVIZER = ((long)Mod) << 31;
    long Data;
    public ModInt1e9p6(long data) { if ((Data = data % Mod) < 0) Data += Mod; }
    public static implicit operator long(ModInt1e9p6 modInt) => modInt.Data;
    public static implicit operator ModInt1e9p6(long val) => new ModInt1e9p6(val);
    public static ModInt1e9p6 operator +(ModInt1e9p6 a, int b) => new ModInt1e9p6() { Data = (a.Data + b + POSITIVIZER) % Mod };
    public static ModInt1e9p6 operator +(ModInt1e9p6 a, long b) => new ModInt1e9p6(a.Data + b);
    public static ModInt1e9p6 operator +(ModInt1e9p6 a, ModInt1e9p6 b) { long res = a.Data + b.Data; return new ModInt1e9p6() { Data = res >= Mod ? res - Mod : res }; }
    public static ModInt1e9p6 operator -(ModInt1e9p6 a, int b) => new ModInt1e9p6() { Data = (a.Data - b + POSITIVIZER) % Mod };
    public static ModInt1e9p6 operator -(ModInt1e9p6 a, long b) => new ModInt1e9p6(a.Data - b);
    public static ModInt1e9p6 operator -(ModInt1e9p6 a, ModInt1e9p6 b) { long res = a.Data - b.Data; return new ModInt1e9p6() { Data = res < 0 ? res + Mod : res }; }
    public static ModInt1e9p6 operator *(ModInt1e9p6 a, int b) => new ModInt1e9p6(a.Data * b);
    public static ModInt1e9p6 operator *(ModInt1e9p6 a, long b) => a * new ModInt1e9p6(b);
    public static ModInt1e9p6 operator *(ModInt1e9p6 a, ModInt1e9p6 b) => new ModInt1e9p6() { Data = a.Data * b.Data % Mod };
    public static bool operator ==(ModInt1e9p6 a, ModInt1e9p6 b) => a.Data == b.Data;
    public static bool operator !=(ModInt1e9p6 a, ModInt1e9p6 b) => a.Data != b.Data;
    public override string ToString() => Data.ToString();
    public override bool Equals(object obj) => (ModInt1e9p6)obj == this;
    public override int GetHashCode() => (int)Data;
}