Problem:
Problem Statement
One day, my grandmas left $N$ cookies.
My elder sister and I were going to eat them immediately, but there was the instruction.
It said
Cookies will go bad; you should eat all of them within $D$ days.
Be careful about overeating; you should eat strictly less than $X$ cookies in a day.
My sister said "
How many ways are there to eat all of the cookies? Let's try counting!
"
Two ways are considered different if there exists a day such that the numbers of the cookies eaten on that day are different in the two ways.
For example, if $N$, $D$ and $X$ are $5$, $2$ and $5$ respectively, the number of the ways is $4$:
Eating $1$ cookie on the first day and $4$ cookies on the second day.
Eating $2$ cookies on the first day and $3$ cookies on the second day.
Eating $3$ cookies on the first day and $2$ cookies on the second day.
Eating $4$ cookies on the first day and $1$ cookie on the second day.
I noticed the number of the ways would be very huge and my sister would die before counting it.
Therefore, I tried to count it by a computer program to save the life of my sister.
Input
The input consists of multiple datasets.
The number of datasets is no more than $100$.
For each dataset, three numbers $N$ ($1 \le N \le 2{,}000$), $D$ ($1 \le D \le 10^{12}$) and $X$ ($1 \le X \le 2{,}000$) are written in a line and separated by a space.
The end of input is denoted by a line that contains three zeros.
Output
Print the number of the ways modulo $1{,}000{,}000{,}007$ in a line for each dataset.
Sample Input
5 2 5
3 3 3
5 4 5
4 1 2
1 5 1
1250 50 50
0 0 0
Output for the Sample Input
4
7
52
0
0
563144298

Submitted solutions:

code_snippet: #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2000+100;
ll pow_m(ll n,ll k)
{
    ll ans=1;
    while(k)
    {
        if(k&1) ans=ans*n%mod;
        n=n*n%mod;
        k>>=1;
    }
    return ans;
}
ll inv[2010];
void init()
{
    inv[0]=inv[1]=1;
    for(int i=2;i<=2000;i++)
    {
        inv[i]=pow_m(i,mod-2);
    }
}
int n,x;
ll d;
ll dp[N][N];
ll f[2][N];

ll C(ll n,ll m)
{
    if(n<m) return 0;
    if(n==m) return 1;
    ll ans=1;
    for(ll i=n;i>=n-m+1;i--)
    {
        ans=(ans*(i%mod))%mod;
    }
    for(int i=1;i<=m;i++)
        ans=(ans*inv[i])%mod;
    //printf("%I64d\n",ans);
    return ans;
}
int main()
{
    init();
    while(scanf("%d%lld%d",&n,&d,&x),n&&d&&x)
    {
        memset(dp,0,sizeof(dp));
        memset(f,0,sizeof(f));
        x--;
        int p=1;
        for(int i=1;i<=n;i++)
        {
            if(i<=x)dp[1][i]=1;
            else dp[1][i]=0;
            f[1][i]=f[1][i-1]+dp[1][i];
        }
        p^=1;
        for(int i=2;i<=n;i++)
        {
            int tmp=min(n,i*x);
            for(int j=1;j<=tmp;j++)
            {
                if(j-x-1>=0){
                        dp[i][j]=(f[p^1][j-1]-f[p^1][j-x-1])%mod;
                        dp[i][j]=(dp[i][j]+mod)%mod;
                }
                else dp[i][j]=f[p^1][j-1]%mod;
                f[p][j]=(f[p][j-1]+dp[i][j])%mod;
            }
            for(int j=tmp+1;j<=n;j++)
            {
                f[p][j]=f[p][j-1];
            }
            p^=1;
        }
//        for(int i=1;i<=n;i++)
//        {
//            for(int j=1;j<=n;j++)
//            {
//                printf("%d ",dp[i][j]);
//            }printf("\n");
//        }
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            ans=(ans+(C(d,i)*dp[i][n])%mod)%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 37820
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

const ll mod = 1e9+7;
int M = 2020;
ll inv[2020];

ll C(ll N, ll K){
	if(N < K)return 0;
	ll ret = 1;
	for(ll i = 0; i < K; i++){
		ret *= (N-i)%mod;
		ret %= mod;
	}
	ret *= inv[K];
	ret %= mod;
	return ret;
}

int solve(){
	ll N,D,X; cin >> N >> D >> X;
	if(N == 0)return 0;
	ll ans = 0;
	for(ll i = 0; i <= N/X; i++){
		ll add = C(D , i);
		add *= C(N-i*X+D-1 , N-i*X);
		add %= mod;
		if(i % 2 == 0){
			ans += add;
		}
		else ans += mod - add;
		ans %= mod;
	}
	cout << ans << endl;
	return 1;
}

ll modpow(ll a, ll b){
	if(b == 0)return 1;
	if(b % 2 == 0){
		ll t = modpow(a, b/2);
		t *= t;
		t %= mod;
		return t;
	}
	ll t = modpow(a, b-1) * a;
	t %= mod;
	return t;
}

int main(){
	ll tmp = 1;
	for(ll i = 1; i < M; i++){
		tmp *= i;
		tmp %= mod;
	}
	inv[M-1] = modpow(tmp , mod-2);
	for(ll i = M-2; i >= 0; i--){
		inv[i] = inv[i+1] * (i+1);
		inv[i] %= mod;
	}
	while(solve());
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3376
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <cassert>

constexpr long long MOD{1000000007};

long long add(long long L, long long R) {
    return (L + R) % MOD;
}

long long inv_plus(long long v) {
    long long res{(v ? MOD - v : 0LL)};
    assert(add(v, res) == 0LL);
    return res;
}

long long sub(long long L, long long R) {
    return add(L, inv_plus(R));
}

long long mult(long long L, long long R) {
    return (L * R) % MOD;
}

long long square(long long v) {
    return mult(v, v);
}

long long pow(long long A, long long e) {
    if (e == 0) return 1LL;
    else if (e & 1) return mult(A, square(pow(A, e >> 1)));
    else return square(pow(A, e >> 1));
}

long long inv_mult(long long v) {
    long long res{pow(v, MOD - 2)};
    assert(mult(v, res) == 1LL);
    return res;
}

long long divm(long long L, long long R) {
    return mult(L, inv_mult(R));
}

bool solve() {
    int N, X;
    long long D;
    std::cin >> N >> D >> X;
    if (N == 0 and D == 0 and X == 0) return false;
    long long ans{};
    std::vector<long long> dp(N + 1);
    dp[0] = 1;
    long long comb{1};
    for (int i{1} ; i <= N ; i++) {
        std::vector<long long> sum(N + 2), next(N + 1);
        for (int j{} ; j <= N ; j++) sum[j + 1] = add(sum[j], dp[j]);
        for (int j{} ; j <= N ; j++) {
            next[j] = sub(sum[j], sum[std::max(0, j - X + 1)]);
        }
        comb = mult(comb, (D - i + 1) % MOD);
        comb = divm(comb, i % MOD);
        dp = std::move(next);
        // for (auto d : dp) std::cout << d << ' ';
        // std::cout << std::endl;
        ans = add(ans, mult(comb, dp[N]));
        if (i == D) break;
    }
    std::cout << ans << '\n';
    return true;
}

int main() {
    while (solve()) ;
}
accuracy: 1
time_ms: 270
memory_kb: 3400
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;

int add(int a, int b) {
    return a + b >= mod ? a + b - mod : a + b;
}
int sub(int a, int b) {
    return a >= b ? a - b : a + mod - b;
}
int mul(int a, int b) {
    return 1ll * a * b  % mod;
}
int inv(int x) {
    return x == 1 ? 1 : sub(0, mul(mod / x, inv(mod % x)));
}

int C(long long n, int k) {
    if (n < 0 || k < 0 || n < k) return 0;
    int ret = 1;
    for (int i = 1; i <= k; ++i) {
        ret = mul(ret, (n - i + 1) % mod);
        ret = mul(ret, inv(i));
    }
    return ret;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int n, x;
    long long d;
    while (cin >> n >> d >> x) {
        if (n == 0 && d == 0 && x == 0) break;
        vector<int> dp(n + 1, 0);
        dp[0] = 1;
        int ret = 0;
        for (int i = 1; i <= n; ++i) {
            vector<int> pref(n + 2, 0);
            for (int j = 0; j <= n; ++j) pref[j + 1] = add(pref[j], dp[j]);
            for (int j = 0; j <= n; ++j) {
                dp[j] = sub(pref[j], pref[max(0, j - x + 1)]);
            }
            ret = add(ret, mul(C(d, i), dp[n]));
        }
        cout << ret << '\n';
    }
}
accuracy: 1
time_ms: 2600
memory_kb: 3256
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

ll MOD = 1000000007;

ll Pow(ll A, ll B) {
    if (B == 0) return 1;
    if (B % 2 == 0) {
        ll X = Pow(A,B/2);
        return (X*X) % MOD;
    }
    return (A*Pow(A,B-1)) % MOD;
}

ll nCr(ll N, ll R) {
    if (R<0||N<R) return 0;
    ll X = 1, Y = 1;
    rep(i,1,R+1) {
        X *= (N+1-i) % MOD;
        X %= MOD;
        Y *= i;
        Y %= MOD;
    }
    return (X * Pow(Y,MOD-2)) % MOD;
}

int main() {
while(1) {
    ll N, D, X;
    cin >> N >> D >> X;
    if (N == 0 && D == 0 && X == 0) return 0;
    vector<ll> A(N+1,0);
    rep(i,0,N+1) {
        if (i%X==0) {
            A[i] = nCr(D,i/X);
            if ((i/X) % 2 == 1) A[i] = (MOD - A[i]) % MOD;
        }
    }
    vector<ll> B(N+1,0);
    rep(i,0,N+1) {
        B[i] = nCr(D+i-1,i);
    }
    ll ANS = 0;
    rep(i,0,N+1) ANS += (A[i] * B[N-i]) % MOD, ANS %= MOD;
    cout << ANS << endl;
}
}
accuracy: 1
time_ms: 940
memory_kb: 3356
final_rank: 6

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 2 "cp-library/src/number/modint.hpp"
struct modinfo { uint mod, root, isprime; };
template < modinfo const &ref >
struct modint {
    static constexpr uint const &mod = ref.mod;
    static constexpr uint const &root = ref.root;
    static constexpr uint const &isprime = ref.isprime;
    uint v = 0;
    constexpr modint& s(uint v) { this->v = v < mod ? v : v - mod; return *this; }
    constexpr modint(ll v = 0) { s(v % mod + mod); }
    modint operator-() const { return modint() - *this; }
    modint& operator+=(const modint& rhs) { return s(v + rhs.v); }
    modint& operator-=(const modint& rhs) { return s(v + mod - rhs.v); }
    modint& operator*=(const modint& rhs) { v = ull(v) * rhs.v % mod; return *this; }
    modint& operator/=(const modint& rhs) { return *this *= inv(rhs); }
    modint operator+(const modint& rhs) const { return modint(*this) += rhs; }
    modint operator-(const modint& rhs) const { return modint(*this) -= rhs; }
    modint operator*(const modint& rhs) const { return modint(*this) *= rhs; }
    modint operator/(const modint& rhs) const { return modint(*this) /= rhs; }
    friend modint pow(modint x, ll n) { modint res(1); while(n > 0) { if(n & 1) res *= x; x *= x; n >>= 1; } return res; }
    friend modint inv(modint v) {
        if(isprime) {
            return pow(v, mod - 2);
        } else {
            ll a = v.v, b = modint::mod, x = 1, y = 0, t;
            while(b > 0) { t = a / b; swap(a -= t * b, b); swap(x -= t * y, y); }
            return modint(x);
        }
    }
    friend modint operator+(int x, const modint& y) { return modint(x) + y; }
    friend modint operator-(int x, const modint& y) { return modint(x) - y; }
    friend modint operator*(int x, const modint& y) { return modint(x) * y; }
    friend modint operator/(int x, const modint& y) { return modint(x) / y; }
    friend istream& operator>>(istream& is, modint& m) { ll x; is >> x; m = modint(x); return is; }
    friend ostream& operator<<(ostream& os, const modint& m) { return os << m.v; }
    bool operator==(const modint& r) const { return v == r.v; }
    bool operator!=(const modint& r) const { return v != r.v; }
    static uint get_mod() { return mod; }
    static int is_prime() { return isprime; }
};
constexpr modinfo base998244353 { 998244353, 3, 1 };
constexpr modinfo base1000000007 { 1000000007, 0, 1 };
using mint998244353 = modint< base998244353 >;
using mint1000000007 = modint< base1000000007 >;
#line 4 "A.cpp"
using mint = mint1000000007;

mint solve(int N, i64 D, int X) {
    vector<mint> f(N + 1, 0);
    f[0] = 1;
    vector<mint> g(X, 1);

    auto mul = [&](vector<mint> a, vector<mint> b) {
        vector<mint> c(N + 1, 0);
        for(int i : rep(int(a.size()))) for(int j : rep(int(b.size()))) {
            if(i + j <= N) c[i + j] += a[i] * b[j];
        }
        return c;
    };

    while(D > 0) {
        if(D % 2 == 1) f = mul(f, g);
        g = mul(g, g);
        D /= 2;
    }
    return f[N];
}

int main() {
    while(true) {
        int N = in(); i64 D = in(); int X = in();
        if(make_tuple(N, D, X) == make_tuple<int,i64,int>(0, 0, 0)) return 0;
        print(solve(N, D, X));
    }
}
accuracy: 1
time_ms: 3250
memory_kb: 3448
final_rank: 18

code_snippet: // author: hanyu
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
static const ll MOD = 1e9 + 7;

struct Comb {
  vector<ll> fac = {1, 1}, inv = {0, 1}, finv = {1, 1}; // inv[0] is undefined
  
  void make(ll n) {
    for (ll i = fac.size(); i <= n; i++) {
      fac.emplace_back(fac[i - 1] * i % MOD);
      inv.emplace_back(MOD - inv[MOD % i] * (MOD / i) % MOD);
      finv.emplace_back(finv[i - 1] * inv[i] % MOD);
    }
    return;
  }
  
  ll nPr(ll n, ll r) {
    if (n < r || n < 0 || r < 0) return 0;
    make(n);
    return fac[n] * finv[n - r] % MOD;
  }

  ll nCr(ll n, ll r) {
    if (n < r || n < 0 || r < 0) return 0;
    if (n < MOD) {
      make(n);
      return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD;
    }
    else { // Lucasの定理
      ll res = 1;
      while (n || r) {
        ll n2 = n % MOD, r2 = r % MOD;
        n /= MOD;
        r /= MOD;
        res = res * nCr(n2, r2) % MOD;
      }
      return res;
    }
  }
} Comb;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    ll n, d, x;
    cin >> n >> d >> x;
    if (n + d + x == 0) break;

    if ((x - 1) * d < n) {
      cout << 0 << endl;
      continue;
    }

    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
    dp[0][0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (dp[i][j] == 0) continue;
        dp[i + 1][j + 1] += dp[i][j];
        if (j + x <= n) dp[i + 1][j + x] += (MOD - dp[i][j]);
      }
      for (int j = 0; j <= n; j++) {
        if (j) dp[i + 1][j] += dp[i + 1][j - 1];
        dp[i + 1][j] %= MOD;
      }
    }

    Comb.make(2010);

    ll ans = 0;
    for (ll j = 1; j <= n; j++) {
      if (j > d) break;
      ll tmp = dp[j][n] % MOD;
      for (ll k = 0; k < j; k++) {
        tmp *= (d - k) % MOD;
        tmp %= MOD;
      }
      tmp *= Comb.finv[j];
      ans += tmp % MOD;
      ans %= MOD;
    }

    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 500
memory_kb: 34484
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
#include <unordered_set>
#include <random>
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
//#define vec vector<ll>
//#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=INF;
const ll inf=INF*INF;
const ll mod=MOD;
const ll inv2=499122177;
const ll MAX=10000010;
const double PI=acos(-1.0);
typedef vector<vector<ll> > mat;
typedef vector<ll> vec;

ll dx[]={0,1,0,-1,-1,-1,1,1};
ll dy[]={1,0,-1,0,-1,1,-1,1};

long long extGCD(long long a,long long mod,long long &x,long long &y){
    if(mod==0){
        x=1;
        y=0;
        return a;
    }
    long long d=extGCD(mod,a%mod,y,x);
    y-=(a/mod)*x;
    return d;
}
long long invmod(long long a,long long mod){
    long long x,y;
    extGCD(a,mod,x,y);
    x%=mod;
    if(x<0)x+=mod;
    return x;
}

void solve(){
    while(1){
        ll N,D,X;cin>>N>>D>>X;
        if((N|D|X)==0)break;
        vector<vector<ll> >dp(N+1,vector<ll>(N+1));
        dp[0][0]=1;
        dp[0][1]=-1;
        rep(i,N+1){
            REP(j,1,N+1){
                dp[i][j] = (dp[i][j] + dp[i][j-1])%mod;
            }
            if(i==N)break;
            rep(j,N){
                dp[i+1][j+1]=(dp[i+1][j+1]+dp[i][j])%mod;
                if(j+X<=N)dp[i+1][j+X]=(dp[i+1][j+X]-dp[i][j])%mod;
            }
        }
        ll now=1;
        ll ans=0;
        REP(i,1,N+1){
            now=now*((D-i+1)%mod)%mod*invmod(i,mod)%mod;
            ans=(ans+dp[i][N]*now%mod)%mod;
        }
        if(ans<0)ans+=mod;
        cout<<ans<<endl;
    }
}

signed main(){
    //cin.tie(0);
    //ios::sync_with_stdio(false);
    solve();
}
accuracy: 1
time_ms: 720
memory_kb: 34316
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

const ll MOD = 1000000007;

ll modpow(ll A, ll N, ll MOD) {
    A %= MOD;
    ll ret = 1;
    while (N) {
        if (N & 1) ret = ret * A % MOD;
        A = A * A % MOD;
        N >>= 1;
    }
    return ret;
}

ll inv(ll x, ll MOD) { return modpow(x, MOD - 2, MOD); }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll N, D, X;
    while (cin >> N >> D >> X) {
        if (N == 0 and D == 0 and X == 0) break;
        V<V<ll>> dp(N + 1, V<ll>(N + 1, 0));
        dp[0][0] = 1;
        rep(i, N) {
            V<ll> cp(N + 2, 0);
            rep(j, N + 1) cp[j + 1] = (cp[j] + dp[i][j]) % MOD;
            rep(j, N + 1) dp[i + 1][j] = (cp[j + 1] - cp[max(0LL, j - X + 2)] + MOD) % MOD;
        }
        D %= MOD;
        ll ans = 0;
        ll C = 1, Cinv = 1;
        for (int m = 1; m <= N; m++) {
            C = C * (D + 1 - m) % MOD;
            Cinv = Cinv * m % MOD;
            ll DCm = C * inv(Cinv, MOD) % MOD;
            ans = (ans + DCm * dp[m][N - m] % MOD) % MOD;
        }
        cout << ans << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 600
memory_kb: 34372
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

template <class Mint> Mint binomial(long long N, long long K) {
    if (N < 0 || K < 0 || N < K) return 0;
    Mint ret1 = 1, ret2 = 1;
    K = min(K, N - K);
    for (long long i = 1; i <= K; i++) {
        ret1 *= N--;
        ret2 *= i;
    }
    return ret1 / ret2;
}


#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif


#include <utility>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

struct barrett {
    unsigned int _m;
    unsigned long long im;

    explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    unsigned int umod() const { return _m; }

    unsigned int mul(unsigned int a, unsigned int b) const {

        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b


        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder


namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder

using mint = atcoder::modint1000000007;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll N, D, X;
    while (cin >> N >> D >> X) {
        if (N == 0 and D == 0 and X == 0) break;
        V<V<mint>> dp(N + 1, V<mint>(N + 1, 0));
        dp[0][0] = 1;
        rep(i, N) {
            V<mint> cp(N + 2, 0);
            rep(j, N + 1) cp[j + 1] = cp[j] + dp[i][j];
            // dp[i][j] -> dp[i+1][j+x]
            rep(j, N + 1) {
                // dp[i+1][j]=sum(dp[i][j-X+2],...,dp[i][j])
                dp[i + 1][j] = cp[j + 1] - cp[max(0LL, j - X + 2)];
            }
        }
        mint ans = 0;
        for (int m = 1; m <= N; m++) {
            if (N - m >= 0) ans += binomial<mint>(D, m) * dp[m][N - m];
        }
        cout << ans.val() << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 18968
final_rank: 10

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < b ? a = b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return b < a ? a = b, true : false;
}
constexpr int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
const double PI = acos(-1.0);
#line 3 "/home/kuhaku/atcoder/github/atcoder-lib/lib/math/modint.hpp"

/**
 * @brief modint
 * @details [参考](https://github.com/ei1333/library/blob/master/math/combinatorics/mod-int.cpp)
 *
 * @tparam mod 法
 */

template <int mod>
struct ModInt {
    constexpr ModInt() noexcept : x(0) {}
    constexpr ModInt(int y) noexcept : x(y >= 0 ? y % mod : (mod - 1 - ~y % mod)) {}
    constexpr ModInt(int64_t y) noexcept : x(y >= 0 ? y % mod : (mod - 1 - ~y % mod)) {}

    constexpr ModInt &operator+=(const ModInt &rhs) noexcept {
        if ((this->x += rhs.x) >= mod) this->x -= mod;
        return *this;
    }
    constexpr ModInt &operator-=(const ModInt &rhs) noexcept {
        if ((this->x += mod - rhs.x) >= mod) this->x -= mod;
        return *this;
    }
    constexpr ModInt &operator*=(const ModInt &rhs) noexcept {
        this->x = (int)(1LL * this->x * rhs.x % mod);
        return *this;
    }
    constexpr ModInt &operator/=(const ModInt &rhs) noexcept {
        *this *= rhs.inverse();
        return *this;
    }

    constexpr ModInt &operator++() noexcept {
        if ((++(this->x)) >= mod) this->x -= mod;
        return *this;
    }
    constexpr ModInt operator++(int) noexcept {
        ModInt tmp(*this);
        this->operator++();
        return tmp;
    }
    constexpr ModInt &operator--() noexcept {
        if ((this->x += mod - 1) >= mod) this->x -= mod;
        return *this;
    }
    constexpr ModInt operator--(int) noexcept {
        ModInt tmp(*this);
        this->operator--();
        return tmp;
    }

    constexpr ModInt operator-() const noexcept { return ModInt(-this->x); }
    constexpr ModInt operator+(const ModInt &rhs) const noexcept { return ModInt(*this) += rhs; }
    constexpr ModInt operator-(const ModInt &rhs) const noexcept { return ModInt(*this) -= rhs; }
    constexpr ModInt operator*(const ModInt &rhs) const noexcept { return ModInt(*this) *= rhs; }
    constexpr ModInt operator/(const ModInt &rhs) const noexcept { return ModInt(*this) /= rhs; }

    constexpr bool operator==(const ModInt &rhs) const noexcept { return this->x == rhs.x; }
    constexpr bool operator!=(const ModInt &rhs) const noexcept { return this->x != rhs.x; }

    explicit operator int() const noexcept { return x; }

    constexpr ModInt inverse() const noexcept {
        int a = x, b = mod, u = 1, v = 0, t;
        while (b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return ModInt(u);
    }

    constexpr ModInt pow(int64_t n) const noexcept { return ModInt(*this).pow_self(n); }
    constexpr ModInt &pow_self(int64_t n) noexcept {
        ModInt res(1);
        for (; n > 0; n >>= 1) {
            if (n & 1) res *= *this;
            *this *= *this;
        }
        *this = res;
        return *this;
    }

    friend istream &operator>>(istream &is, ModInt &rhs) {
        int64_t t;
        is >> t;
        rhs = ModInt<mod>(t);
        return (is);
    }
    friend ostream &operator<<(ostream &os, const ModInt &rhs) { return os << rhs.x; }

    static int get_mod() noexcept { return mod; }

  private:
    int x;
};
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#line 6 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
using ll = int64_t;
using ld = long double;
#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FOR(i, 1, n+1)
#define repr(i, n) FORR(i, n, 0)
#define repnr(i, n) FORR(i, n+1, 1)
#define all(s) (s).begin(), (s).end()
template<class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os<<'('<<p.first<< ','<<p.second<<')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?"":" ")<<*it; } return os;
}
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cout<<head<<'\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);
}
template<typename T, typename... Args>
auto make_vector(T x, int arg, Args ...args) {
    if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));
}
void sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }
void setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }
void Yes(bool is_correct) { std::cout<<(is_correct?"Yes":"No")<<std::endl; }
void YES(bool is_correct) { std::cout<<(is_correct?"YES":"NO")<<std::endl; }
#line 4 "a.cpp"

using Mint = ModInt<MOD>;

int main(void) {
    sonic();
    while (true) {
        int n, x;
        ll d;
        cin >> n >> d >> x;
        if (!n)
            break;

        vector<vector<Mint>> dp(n + 1, vector<Mint>(n + 1));
        dp[0][0] = 1;
        rep(i, n) {
            Mint sum = 0;
            rep(j, n) {
                sum += dp[i][j];
                if (j >= x - 1)
                    sum -= dp[i][j - x + 1];
                dp[i + 1][j + 1] += sum;
            }
        }

        Mint ans = 0;
        Mint combi = 1;
        rep(i, n + 1) {
            ans += dp[i][n] * combi;
            combi *= d - i;
            combi /= i + 1;
        }
        co(ans);
    }

    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 18900
final_rank: 7

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
const ll MOD = (ll)1e9+7;
//const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;

    void print();
    template <class t> void print(const vector<t> &);
    template <class t, class u> void print(const pair<t, u> &);
    template <class t> void print(const t&);
    template <class Head, class... Tail> void print(const Head&, const Tail&...);

    template <class t> void println(const vector<vector<t>>&);
    template <class t> void println(const vector<t>&);
    template <class t> void println(const t&);
    template <class Head, class... Tail> void println(const Head&, const Tail&...);
    void println();
    void newline();

    void print(){
        return;
    }

    template <class t>
    void print(const vector<t>&x){
        for(const t&i:x)print(i);
    }
    template <class t, class u>
    void print(const pair<t,u>&p){
        print(p.first);
        print(p.second);
    }
    template <class t>
    void print(const t&x){
        if(was_output)cout<<" ";
        cout<<x;
        was_output = true;
    }
    template <class Head, class... Tail>
    void print(const Head&head,const Tail&...tail){
        print(head);
        print(tail...);
    }

    template<class t>
    void println(const vector<vector<t>>&x){
        for(vector<t> i:x)println(i);
    }
    template<class t>
    void println(const vector<t>&x){
        for(const t&i:x)print(i);
        println();
    }
    template <class t>
    void println(const t&x){
        print(x);
        println();
    }
    void println(){
        if(was_output){
            cout << endl;
            was_output = false;
        }
    }
    template <class Head, class... Tail>
    void println(const Head&head,const Tail&...tail){
        print(head);
        println(tail...);
    }

    void newline(){
        was_output = true;
        println();
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

    template<class t>
    ostream& operator<<(ostream&os, vector<t> &v){
        os << "{";
        for(t &i:v){
            os << i << ", ";
        }
        os << "}";
        return os;
    }

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

    long long lcm(long long a,long long b){
        return a / gcd(a,b) * b;
    }

    class output_initializer{
    public:
        output_initializer(){
            ios::sync_with_stdio(false);
            cin.tie(nullptr);
            cout << setprecision(20);
        }
    };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();
}

using namespace templates;


template<long long mod>
class modint{
	public:
		long long x;
		modint(long long a){x=a%mod;if(x<0)x+=mod;}
		modint(){x=0;}

		modint pow(long long a){
			modint res(1), b(x);
			while(a){
				if(a&1)res*=b;
				b*=b;
				a>>=1;
			}
			return res;
		}

		modint inv(){return pow(mod-2);}

		modint& operator+=(modint a){x=(x+a.x)%mod;return *this;}
		modint& operator-=(modint a){x=x-a.x;if(x<0)x+=mod;return *this;}
		modint& operator*=(modint a){x=x*a.x%mod;return *this;}
		modint& operator/=(modint a){x=x*a.inv().x%mod;return *this;}

		modint operator+(modint a){return modint(x)+=a;}
		modint operator-(modint a){return modint(x)-=a;}
		modint operator*(modint a){return modint(x)*=a;}
		modint operator/(modint a){return modint(x)/=a;}

		modint operator-(){return modint(x);}

		bool operator==(const modint a){return x == a.x;}
		bool operator<(const modint a){return x < a.x;}
		bool operator>(const modint a){return x > a.x;}
};

template<long long mod>
ostream& operator<<(ostream& os, const modint<mod>& a){
	os << a.x;
	return os;
}

using mint = modint<MOD>;

mint combination(ll a,ll b){
    if(a >= MOD or b >= MOD){
        return combination(a % MOD, b % MOD) * combination(a / MOD, b / MOD);
    }
    mint res(1);
    mint div(1);
    rep(i,b){
        res *= a - i;
        div *= i + 1;
    }
    return res / div;
}

mint func(int n,ll d,ll x){
    vvector<mint> dp(n+1,vector<mint>(n+1));
    vvector<bool> used(n+1,vector<bool>(n+1,false));
    method(rec,mint,int rem,int eat){
        if(rem==0)return eat==0;
        mint &it = dp[rem][eat];
        if(used[rem][eat])return  it;
        used[rem][eat] = true;
        it = 0;
        if(eat>=1){
            it += rec(rem,eat-1);
            it += rec(rem-1,eat-1);
        }
        if(eat >= x){
            it -= rec(rem-1,eat-x);
        }
        return it;
    };
    mint res(0);
    rep(i,1,min<ll>(d+1,n+1)){
        res += rec(i,n) * combination(d,i);
    }
    return res;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    while(true){
        int n = in();
        ll d = in();
        ll x = in();
        if(n==0)break;
        println(func(n,d,x));
    }
    return 0;
}
accuracy: 1
time_ms: 1040
memory_kb: 35192
final_rank: 19

code_snippet: #include <cstdio>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

const int mod = 1e9 + 7;

int add(int u, int v) {
  int res = u + v;
  if (res >= mod) res -= mod;
  return res; 
}

struct ModInt {
	int n,mod;
	vector<int> f;
	vector<int> invf;
	ModInt(int n,int mod):n(n),mod(mod){
		f.resize(n+1);
		invf.resize(n+1);
		preWork(n);
	}
	void preWork(int n){
		f[0] = 1;
		for(int i = 1; i <= n; i++) f[i] = f[i-1]*1ll*i%mod;
		invf[n] = power(f[n],mod-2);
		for(int i = n-1; i >= 0; i--) invf[i] = invf[i+1]*1ll*(i+1)%mod;
	}
	int power(int a,int b){
		int res = 1;
		while(b){
			if(b&1) res = res*1ll*a%mod;
			a = a*1ll*a%mod;
			b /= 2;
		}
		return res;
	}
	int comb(int n,int m){
		return f[n]*1ll*invf[m]%mod*invf[n-m]%mod;
	}
};
auto md = ModInt(2000, mod);

int comb(long long n, long long m) {  
  int inv = md.invf[m];
  for (long long i = n; i > n - m; i--) inv = inv * 1ll * (i % mod) % mod;
  // printf("n = %lld, m = %lld, res = %lld\n", n, m, inv);
  return inv;
}

int main() {
  // sum is n
  // smaller than x
  // within d day
long long n, d, x;
while (scanf("%lld%lld%lld", &n, &d, &x) == 3) {
  if (n == 0 && d == 0 && x == 0) break;
  vector<vector<int>> f(n + 1, vector<int> (n + 1, 0));
  vector<int> pre(n + 1, 0);
  f[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      pre[j] = f[i - 1][j]; 
      if (j - 1 >= 0) pre[j] = add(pre[j], pre[j - 1]);
    }
    f[i][0] = 0;
    for (int j = 1; j <= n; j++) {
      int low = j - x;
      f[i][j] = pre[j - 1];
      if (low >= 0) {
        f[i][j] -= pre[low];
        if (f[i][j] < 0) f[i][j] += mod;  
      }
    }
  }
  int ans = 0;
  for (int u = 1; u <= min(d, (long long)n); u++) {
    int way = comb(d, u);
    //printf("way = %d\n", way);
    //printf("f[%d][%d] = %d\n", u, n, f[u][n]);
    way = way * 1ll * f[u][n] % mod;
    ans = add(ans, way); 
  }
  printf("%d\n", ans);
}
  return 0; 
}
accuracy: 1
time_ms: 380
memory_kb: 18304
final_rank: 9

code_snippet: #include <bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
using ll = long long;
#define all(A) A.begin(),A.end()
using vll = vector<ll>;
using vvll = vector<vll>;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)

vector<ll> fact, factinv, inv;
ll mod = 1e9 + 7;
void prenCkModp(ll n) {
	fact.resize(n + 5);
	factinv.resize(n + 5);
	inv.resize(n + 5);
	fact.at(0) = fact.at(1) = 1;
	factinv.at(0) = factinv.at(1) = 1;
	inv.at(1) = 1;
	for (ll i = 2; i < n + 5; i++) {
		fact.at(i) = (fact.at(i - 1) * i) % mod;
		inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
		factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
	}

}
ll nCk(ll n, ll k) {
	if (n < k) return 0;
	return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}

int main() {
    prenCkModp(100000);
    while(1){
        ll N,D,X;
        cin>>N>>D>>X;
        if(N==0)return 0;
        ll an=0;
        rep(i,min(N,D)+1){
             if(N<i*X)break;
             ll p=N-i*X;
             ll k=1;
             rep(j,p){
                 k*=(N-i*X+D-1-j)%mod;
                 k%=mod;
                 k*=inv[j+1];
                 k%=mod;
             }
             rep(j,i){
                 k*=(D-j)%mod;
                 k%=mod;
                 k*=inv[j+1];
                 k%=mod;
             }
             an+=k*(i%2==0?1:mod-1);
             an%=mod;

        }
        cout<<(an+mod)%mod<<endl;
    }

}
accuracy: 1
time_ms: 140
memory_kb: 5072
final_rank: 3

code_snippet: #include<bits/stdc++.h>

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

using namespace std;
#define INFS (1LL<<28)
#define DEKAI 1000000007
//#define INF 1000000007
//#define MOD 1000000007
#define lp(i,n) for(int i=0;i<(long long)n;i++)
#define lps(i,n) for(int i=1;i<=n;i++)
#define all(c) begin(c), end(c)

//#define int long long

namespace {
#define __DECLARE__(C)    \
	    template <typename T> \
	std::ostream &operator<<(std::ostream &, const C<T> &);

#define __DECLAREM__(C)               \
	    template <typename T, typename U> \
	std::ostream &operator<<(std::ostream &, const C<T, U> &);

    __DECLARE__(std::vector)
    __DECLARE__(std::deque)
    __DECLARE__(std::set)
    __DECLARE__(std::stack)
    __DECLARE__(std::queue)
    __DECLARE__(std::priority_queue)
    __DECLARE__(std::unordered_set)
    __DECLAREM__(std::map)
    __DECLAREM__(std::unordered_map)

    template <typename T, typename U>
    std::ostream &operator<<(std::ostream &, const std::pair<T, U> &);
    template <typename... T>
    std::ostream &operator<<(std::ostream &, const std::tuple<T...> &);
    template <typename T, std::size_t N>
    std::ostream &operator<<(std::ostream &, const std::array<T, N> &);

    template <typename Tuple, std::size_t N>
    struct __TuplePrinter__ {
        static void print(std::ostream &os, const Tuple &t) {
            __TuplePrinter__<Tuple, N - 1>::print(os, t);
            os << ", " << std::get<N - 1>(t);
        }
    };

    template <typename Tuple>
    struct __TuplePrinter__<Tuple, 1> {
        static void print(std::ostream &os, const Tuple &t) { os << std::get<0>(t); }
    };

    template <typename... T>
    std::ostream &operator<<(std::ostream &os, const std::tuple<T...> &t) {
        os << '(';
        __TuplePrinter__<decltype(t), sizeof...(T)>::print(os, t);
        os << ')';
        return os;
    }

    template <typename T, typename U>
    std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &v) {
        return os << '(' << v.first << ", " << v.second << ')';
    }

#define __INNER__                             \
	os << '[';                                \
	for (auto it = begin(c); it != end(c);) { \
		os << *it;                            \
		os << (++it != end(c) ? ", " : "");   \
	}                                         \
	return os << ']';

    template <typename T, std::size_t N>
    std::ostream &operator<<(std::ostream &os, const std::array<T, N> &c) {
        __INNER__
    }

#define __DEFINE__(C)                                           \
	    template <typename T>                                       \
	std::ostream &operator<<(std::ostream &os, const C<T> &c) { \
		__INNER__                                               \
	}

#define __DEFINEM__(C)                                             \
	    template <typename T, typename U>                              \
	std::ostream &operator<<(std::ostream &os, const C<T, U> &c) { \
		__INNER__                                                  \
	}

#define __DEFINEW__(C, M1, M2)                                  \
	    template <typename T>                                       \
	std::ostream &operator<<(std::ostream &os, const C<T> &c) { \
		std::deque<T> v;                                        \
		for (auto d = c; !d.empty(); d.pop()) v.M1(d.M2());     \
			return os << v;                                         \
	}

    __DEFINE__(std::vector)
    __DEFINE__(std::deque)
    __DEFINE__(std::set)
    __DEFINEW__(std::stack, push_front, top)
    __DEFINEW__(std::queue, push_back, front)
    __DEFINEW__(std::priority_queue, push_front, top)
    __DEFINE__(std::unordered_set)
    __DEFINEM__(std::map)
    __DEFINEM__(std::unordered_map)
}

#define pii pair<int,int>
#define ll long long
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// modint
template <signed M, unsigned T>
struct mod_int {
    constexpr static signed MODULO = M;
    constexpr static unsigned TABLE_SIZE = T;

    signed x;

    mod_int() : x(0) {}

    mod_int(long long y) : x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {}

    mod_int(int y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {}

    mod_int &operator+=(const mod_int &rhs) {
        if ((x += rhs.x) >= MODULO) x -= MODULO;
        return *this;
    }

    mod_int &operator-=(const mod_int &rhs) {
        if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO;
        return *this;
    }

    mod_int &operator*=(const mod_int &rhs) {
        x = static_cast<signed>(1LL * x * rhs.x % MODULO);
        return *this;
    }

    mod_int &operator/=(const mod_int &rhs) {
        x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO);
        return *this;
    }

    mod_int operator-() const { return mod_int(-x); }

    mod_int operator+(const mod_int &rhs) const { return mod_int(*this) += rhs; }

    mod_int operator-(const mod_int &rhs) const { return mod_int(*this) -= rhs; }

    mod_int operator*(const mod_int &rhs) const { return mod_int(*this) *= rhs; }

    mod_int operator/(const mod_int &rhs) const { return mod_int(*this) /= rhs; }

    bool operator<(const mod_int &rhs) const { return x < rhs.x; }

    mod_int inv() const {
        assert(x != 0);
        if (x <= static_cast<signed>(TABLE_SIZE)) {
            if (_inv[1].x == 0) prepare();
            return _inv[x];
        } else {
            signed a = x, b = MODULO, u = 1, v = 0, t;
            while (b) {
                t = a / b;
                a -= t * b;
                std::swap(a, b);
                u -= t * v;
                std::swap(u, v);
            }
            return mod_int(u);
        }
    }

    mod_int pow(long long t) const {
        assert(!(x == 0 && t == 0));
        mod_int e = *this, res = mod_int(1);
        for (; t; e *= e, t >>= 1)
            if (t & 1) res *= e;
        return res;
    }

    mod_int fact() {
        if (_fact[0].x == 0) prepare();
        return _fact[x];
    }

    mod_int inv_fact() {
        if (_fact[0].x == 0) prepare();
        return _inv_fact[x];
    }

    mod_int choose(mod_int y) {
        assert(y.x <= x);
        return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact();
    }

    static mod_int _inv[TABLE_SIZE + 1];

    static mod_int _fact[TABLE_SIZE + 1];

    static mod_int _inv_fact[TABLE_SIZE + 1];

    static void prepare() {
        _inv[1] = 1;
        for (int i = 2; i <= (int)TABLE_SIZE; ++i) {
            _inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO;
        }
        _fact[0] = 1;
        for (unsigned i = 1; i <= TABLE_SIZE; ++i) {
            _fact[i] = _fact[i - 1] * int(i);
        }
        _inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv();
        for (int i = (int)TABLE_SIZE - 1; i >= 0; --i) {
            _inv_fact[i] = _inv_fact[i + 1] * (i + 1);
        }
    }
};

template <int M, unsigned F>
std::ostream &operator<<(std::ostream &os, const mod_int<M, F> &rhs) {
    return os << rhs.x;
}

template <int M, unsigned F>
std::istream &operator>>(std::istream &is, mod_int<M, F> &rhs) {
    long long s;
    is >> s;
    rhs = mod_int<M, F>(s);
    return is;
}

template <int M, unsigned F>
mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1];

template <int M, unsigned F>
mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1];

template <int M, unsigned F>
mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1];

template <int M, unsigned F>
bool operator==(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) {
    return lhs.x == rhs.x;
}

template <int M, unsigned F>
bool operator!=(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) {
    return !(lhs == rhs);
}

const int MF = 1000010;
const int MOD = 1000000007;

using mint = mod_int<MOD, MF>;

mint binom(int n, int r) { return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r); }

mint fact(int n) { return mint(n).fact(); }

mint inv_fact(int n) { return mint(n).inv_fact(); }
const ll mod = 1000000007;
// const int MAX_N = 1024; // 4MB
// nCr % mod

inline ll gcds(ll a, ll b) { return b ? gcds(b, a % b) : a; }
inline ll lcms(ll a, ll b) { return a / gcd(a, b) * b; }

template <typename Int, Int MOD, int N>
struct comb_util {
    std::array<Int, N + 1> fc, ifc;

    comb_util() {
        fc[0] = 1;
        for (int i = 1; i <= N; i++) fc[i] = fc[i - 1] * i % MOD;
        ifc[N] = inv(fc[N]);
        for (int i = N - 1; i >= 0; i--) ifc[i] = ifc[i + 1] * (i + 1) % MOD;
    }

    Int fact(Int n) { return fc[n]; }

    Int inv_fact(Int n) { return ifc[n]; }

    Int inv(Int n) { return pow(n, MOD - 2); }

    Int pow(Int n, Int a) {
        Int res = 1, exp = n;
        for (; a; a /= 2) {
            if (a & 1) res = res * exp % MOD;
            exp = exp * exp % MOD;
        }
        return res;
    }

    Int perm(Int n, Int r) {
        if (r < 0 || n < r)
            return 0;
        else
            return fc[n] * ifc[n - r] % MOD;
    }

    Int binom(Int n, Int r) {
        if (n < 0 || r < 0 || n < r) return 0;
        return fc[n] * ifc[r] % MOD * ifc[n - r] % MOD;
    }

    Int homo(Int n, Int r) {
        if (n < 0 || r < 0) return 0;
        return r == 0 ? 1 : binom(n + r - 1, r);
    }
};

using comb = comb_util<long long, 1000000007, 3000000>;

#define RK 200000000000
#define LK 300000000000
#define PL 400000000000
#define MI 500000000000
#define KA 600000000000
#define PI acos(-1)
#define int long long
mint dp[2100][2100];
mint sum[2100][2100];


signed main(){
    while(1){
        int n,d,x;
        cin>>n>>d>>x;
        if(n==0)return 0;
        dp[0][0]=1;
        for(int i=1;i<=n;i++){
            lp(j,n+1){
                if(j==0)sum[i][j]=dp[i-1][j];
                else{
                    sum[i][j]=dp[i-1][j]+sum[i][j-1];
                    dp[i][j]=sum[i][j-1];
                    if(j>=x)dp[i][j]-=sum[i][j-x];
                }
                //cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
            }
        }

        mint ans = 0;
        mint up = 1;
        mint down = 1;
        for(int i=1;i<=min(n, d);i++){
            //dCi*dp[i][n]
            down*=i;
            up*=(d-i+1);
            ans +=up/down*dp[i][n];
        }
        cout<<ans<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 49552
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
long long n, d, x;
const long long MOD = 1000000007;

long long modpow(long long a, long long b){
    long long ret = 1;
    while(b){
        if(b & 1){
            ret *= a;
            ret %= MOD;
        }
        a *= a;
        a %= MOD;
        b >>= 1;
    }
    return ret;
}

void solve(){
    vector<vector<long long>> dp(n + 1, vector<long long>(n + 1, 0));
    dp[0][0] = 1;
    vector<long long> cum(n + 1, 1);
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n; j++){
            if(j - x < 0) dp[i][j] = cum[j - 1];
            else{
                dp[i][j] = cum[j - 1] - cum[j - x];
                if(dp[i][j] < 0) dp[i][j] += MOD;
            }
        }
        cum[0] = 0;
        for(int j = 1; j <= n; j++){
            cum[j] = cum[j - 1] + dp[i][j];
            if(cum[j] >= MOD) cum[j] -= MOD;
        }
    }
    long long ans = 0;
    long long comb = 1;
    for(long long i = 1; i <= n; i++){
        if(i > d) break;
        comb *= (d - i + 1) % MOD;
        comb %= MOD;
        comb *= modpow(i, MOD - 2);
        comb %= MOD;
        ans += comb * dp[i][n] % MOD;
        ans %= MOD;
    }
    cout << ans << "\n";
}

int main(void){
    while(1){
        cin >> n >> d >> x;
        if(n == 0) break;
        solve();
    }
}
accuracy: 1
time_ms: 310
memory_kb: 34328
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (int i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"YES\n"; else cout<<"NO\n";}

ll mypow(ll a,ll b){
	if(b==0) return 1;
	if(b%2==0) return mypow((a*a)%mod,b/2);
	return (a*mypow(a,b-1))%mod;
}

ll N,D,X;
void solve();

//  rainy ~ 雨に打たれて ~
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	while(cin>>N>>D>>X,X) solve();
}

void solve(){
	vector<vector<ll>> dp(N+1,vector<ll>(N+1));
	dp[0][0]=1;
	rep(i,N){
		ll tmp=0;
		rep(j,N+1){
			if(j-X>=0) tmp-=dp[i][j-X];
			tmp%=mod;
			dp[i+1][j]=tmp;
			tmp+=dp[i][j];
		}
	}
	ll ans=0;
	ll base=1;
	D%=mod;
	rep(i,N){
		base*=(D-i);
		base%=mod;
		base*=mypow(i+1,mod-2);
		base%=mod;
		ans+=(base*dp[i+1][N])%mod;
		ans%=mod;
	}
	cout<<(ans+mod)%mod<<"\n";
}
accuracy: 1
time_ms: 470
memory_kb: 34372
final_rank: 13

code_snippet: #include "bits/stdc++.h"

#include <algorithm>
#include <array>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder




using namespace atcoder;

#include <utility>

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m < 2^31`
    barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

}  // namespace internal

}  // namespace atcoder


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }
    static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }
    dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder


namespace atcoder {

    namespace internal {

        template <class mint, internal::is_static_modint_t<mint>* = nullptr>
        void butterfly(std::vector<mint>& a) {
            static constexpr int g = internal::primitive_root<mint::mod()>;
            int n = int(a.size());
            int h = internal::ceil_pow2(n);

            static bool first = true;
            static mint sum_e[30];  // sum_e[i] = ies[0] * ... * ies[i - 1] * es[i]
            if (first) {
                first = false;
                mint es[30], ies[30];  // es[i]^(2^(2+i)) == 1
                int cnt2 = bsf(mint::mod() - 1);
                mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv();
                for (int i = cnt2; i >= 2; i--) {
                    // e^(2^i) == 1
                    es[i - 2] = e;
                    ies[i - 2] = ie;
                    e *= e;
                    ie *= ie;
                }
                mint now = 1;
                for (int i = 0; i <= cnt2 - 2; i++) {
                    sum_e[i] = es[i] * now;
                    now *= ies[i];
                }
            }
            for (int ph = 1; ph <= h; ph++) {
                int w = 1 << (ph - 1), p = 1 << (h - ph);
                mint now = 1;
                for (int s = 0; s < w; s++) {
                    int offset = s << (h - ph + 1);
                    for (int i = 0; i < p; i++) {
                        auto l = a[i + offset];
                        auto r = a[i + offset + p] * now;
                        a[i + offset] = l + r;
                        a[i + offset + p] = l - r;
                    }
                    now *= sum_e[bsf(~(unsigned int)(s))];
                }
            }
        }

        template <class mint, internal::is_static_modint_t<mint>* = nullptr>
        void butterfly_inv(std::vector<mint>& a) {
            static constexpr int g = internal::primitive_root<mint::mod()>;
            int n = int(a.size());
            int h = internal::ceil_pow2(n);

            static bool first = true;
            static mint sum_ie[30];  // sum_ie[i] = es[0] * ... * es[i - 1] * ies[i]
            if (first) {
                first = false;
                mint es[30], ies[30];  // es[i]^(2^(2+i)) == 1
                int cnt2 = bsf(mint::mod() - 1);
                mint e = mint(g).pow((mint::mod() - 1) >> cnt2), ie = e.inv();
                for (int i = cnt2; i >= 2; i--) {
                    // e^(2^i) == 1
                    es[i - 2] = e;
                    ies[i - 2] = ie;
                    e *= e;
                    ie *= ie;
                }
                mint now = 1;
                for (int i = 0; i <= cnt2 - 2; i++) {
                    sum_ie[i] = ies[i] * now;
                    now *= es[i];
                }
            }

            for (int ph = h; ph >= 1; ph--) {
                int w = 1 << (ph - 1), p = 1 << (h - ph);
                mint inow = 1;
                for (int s = 0; s < w; s++) {
                    int offset = s << (h - ph + 1);
                    for (int i = 0; i < p; i++) {
                        auto l = a[i + offset];
                        auto r = a[i + offset + p];
                        a[i + offset] = l + r;
                        a[i + offset + p] =
                                (unsigned long long)(mint::mod() + l.val() - r.val()) *
                                inow.val();
                    }
                    inow *= sum_ie[bsf(~(unsigned int)(s))];
                }
            }
        }

    }  // namespace internal

template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(std::vector<mint> a, std::vector<mint> b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};
    if (std::min(n, m) <= 60) {
        if (n < m) {
            std::swap(n, m);
            std::swap(a, b);
        }
        std::vector<mint> ans(n + m - 1);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                ans[i + j] += a[i] * b[j];
            }
        }
        return ans;
    }
    int z = 1 << internal::ceil_pow2(n + m - 1);
    a.resize(z);
    internal::butterfly(a);
    b.resize(z);
    internal::butterfly(b);
    for (int i = 0; i < z; i++) {
        a[i] *= b[i];
    }
    internal::butterfly_inv(a);
    a.resize(n + m - 1);
    mint iz = mint(z).inv();
    for (int i = 0; i < n + m - 1; i++) a[i] *= iz;
    return a;
}

template <unsigned int mod = 998244353,
        class T,
        std::enable_if_t<internal::is_integral<T>::value>* = nullptr>
std::vector<T> convolution(const std::vector<T>& a, const std::vector<T>& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};

    using mint = static_modint<mod>;
    std::vector<mint> a2(n), b2(m);
    for (int i = 0; i < n; i++) {
        a2[i] = mint(a[i]);
    }
    for (int i = 0; i < m; i++) {
        b2[i] = mint(b[i]);
    }
    auto c2 = convolution(move(a2), move(b2));
    std::vector<T> c(n + m - 1);
    for (int i = 0; i < n + m - 1; i++) {
        c[i] = c2[i].val();
    }
    return c;
}

std::vector<long long> convolution_ll(const std::vector<long long>& a,
                                      const std::vector<long long>& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};

    static constexpr unsigned long long MOD1 = 754974721;  // 2^24
    static constexpr unsigned long long MOD2 = 167772161;  // 2^25
    static constexpr unsigned long long MOD3 = 469762049;  // 2^26
    static constexpr unsigned long long M2M3 = MOD2 * MOD3;
    static constexpr unsigned long long M1M3 = MOD1 * MOD3;
    static constexpr unsigned long long M1M2 = MOD1 * MOD2;
    static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3;

    static constexpr unsigned long long i1 =
            internal::inv_gcd(MOD2 * MOD3, MOD1).second;
    static constexpr unsigned long long i2 =
            internal::inv_gcd(MOD1 * MOD3, MOD2).second;
    static constexpr unsigned long long i3 =
            internal::inv_gcd(MOD1 * MOD2, MOD3).second;

    auto c1 = convolution<MOD1>(a, b);
    auto c2 = convolution<MOD2>(a, b);
    auto c3 = convolution<MOD3>(a, b);

    std::vector<long long> c(n + m - 1);
    for (int i = 0; i < n + m - 1; i++) {
        unsigned long long x = 0;
        x += (c1[i] * i1) % MOD1 * M2M3;
        x += (c2[i] * i2) % MOD2 * M1M3;
        x += (c3[i] * i3) % MOD3 * M1M2;
        // B = 2^63, -B <= x, r(real value) < B
        // (x, x - M, x - 2M, or x - 3M) = r (mod 2B)
        // r = c1[i] (mod MOD1)
        // focus on MOD1
        // r = x, x - M', x - 2M', x - 3M' (M' = M % 2^64) (mod 2B)
        // r = x,
        //     x - M' + (0 or 2B),
        //     x - 2M' + (0, 2B or 4B),
        //     x - 3M' + (0, 2B, 4B or 6B) (without mod!)
        // (r - x) = 0, (0)
        //           - M' + (0 or 2B), (1)
        //           -2M' + (0 or 2B or 4B), (2)
        //           -3M' + (0 or 2B or 4B or 6B) (3) (mod MOD1)
        // we checked that
        //   ((1) mod MOD1) mod 5 = 2
        //   ((2) mod MOD1) mod 5 = 3
        //   ((3) mod MOD1) mod 5 = 4
        long long diff =
                c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1));
        if (diff < 0) diff += MOD1;
        static constexpr unsigned long long offset[5] = {
                0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3};
        x -= offset[diff % 5];
        c[i] = x;
    }

    return c;
}

}  // namespace atcoder



using namespace std;
using namespace atcoder;
using ll = long long;
using vl = vector<long long>;
using pl = pair<long long, long long>;
constexpr int dx[8]{1,0,-1,0,1,1,-1,-1};
constexpr int dy[8]{0,1,0,-1,1,-1,1,-1};
#define rep(i, n) for(long long (i) = 0; (i) < (n); (i)++)
#define reps(i, a, b) for(long long (i) = (a); (i) < (b); (i)++)
#define rrep(i, a, b) for(long long (i) = (a); (i) >= (b); (i)--)
#define all(v) (v).begin(), (v).end()
#define eb emplace_back
#define V vector
constexpr long long MOD1 = 1000000007;
constexpr long long MOD9 =  998244353;
constexpr long long INF = numeric_limits<long long>::max() / 8;
long long MOD(long long n, long long m){if(n >= 0) {return n % m;} else return (n%m + m)%m;}
long long POW(long long x,long long n) {ll ret=1;while(n>0){if(n&1){ret=ret*x;}if(n>>=1){x=x*x;}}return ret;}
template<typename T> T div_ceil(T a,T b){return (a - 1) / b + 1;}
template<typename T> T SUM(const vector<T> &v){T ret{};return(accumulate(v.begin(), v.end(), ret));}
template<typename T> T MAX(const vector<T> &v){return *max_element(v.begin(), v.end());}
template<typename T> T MIN(const vector<T> &v){return *min_element(v.begin(), v.end());}
template<typename T> T POWMOD(T x, long long n, long long m) {T ret=1;while(n>0){if(n&1)ret=ret*x%m;x=x*x%m;n>>=1;}return ret%m;}
template<typename T, typename U> bool contains(T S, U x){return (S & (1 << x)) != 0;}
template<typename T> void rsort(vector<T> &v) {sort(v.begin(), v.end(), greater<T>());}
template<typename T> bool chmax(T &a, const T& b) {if(a<b){a=b;return true;}return false;}
template<typename T> bool chmin(T &a, const T& b) {if(a>b){a=b;return true;}return false;}
template<typename T> int popcount(T x){return __builtin_popcount(x);}
template<typename T> void uniquify(vector<T> &v){sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}
template<typename T,typename S> ostream&operator<<(ostream&os,const pair<T,S>&p){os<<"("<<p.first<<", "<<p.second<<")";return os;}
template<typename T,typename S> ostream&operator<<(ostream&os,const map<T,S>&ma){for(auto [a,b]:ma){os<<"("<<a<<", "<<b<<")"<<" ";}return os;}
template<typename T> ostream &operator<<(ostream&os,const set<T>&s){os<<"{";for(auto c:s)os<<c<<" ";os<<"}";return os;}
template<typename T> ostream &operator<<(ostream&os,const vector<T>&v){os<<"[";for(int i=0;i<v.size();i++){os<<v[i]<<(i==v.size()-1?"":" ");}os<<"]";return os;}
template<typename T> pair<vector<int>, vector<T> > compress(vector<T> &a){
    int n = a.size(); vector<T> val = a;
    vector<int> ord(n);
    sort(val.begin(), val.end()); val.erase(unique(val.begin(), val.end()), val.end());
    for(int i = 0; i < n; i++) ord[i] = lower_bound(a.begin(), a.end(), val[i]) - a.begin();
    return {ord, val};
}

//#define ONLINE_JUDGE
#ifdef ONLINE_JUDGE
#define dump(x) true
#else
#define dump(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#endif

using mint = modint1000000007;
std::istream &operator>>(std::istream& is, modint1000000007 &m){
    int a; is >> a; m = a;
    return is;
}
std::ostream &operator<<(std::ostream& os, const modint1000000007 &m){
    os << m.val();
    return os;
}


vector<modint1000000007> convolution1000000007(const vector<modint1000000007> &a, const vector<modint1000000007> &b){
    using mint1 = static_modint<167772161>;
    using mint2 = static_modint<469762049>;
    using mint3 = static_modint<1224736769>;
    int n = (int)a.size(), m = (int)b.size();
    vector<long long> a_int(n), b_int(m);
    for(int i = 0; i < n; i++) a_int[i] = a[i].val();
    for(int i = 0; i < m; i++) b_int[i] = b[i].val();
    vector<long long> x = atcoder::convolution<167772161>(a_int, b_int);
    vector<long long> y = atcoder::convolution<469762049>(a_int, b_int);
    vector<long long> z = atcoder::convolution<1224736769>(a_int, b_int);
    vector<modint1000000007> ret(n + m - 1);
    constexpr int m1_inv_mod_m2 = 104391568;
    constexpr int m1m2_inv_mod_m3 = 721017874;
    constexpr int m1m2_mod_1000000007 = 564826938;
    for(int i = 0; i < n + m - 1; i++){
        long long v1 = ((y[i] - x[i]) * (mint2)m1_inv_mod_m2).val();
        long long v2 = ((z[i] - ((mint3)(x[i] + 167772161 * v1)).val()) * mint3(m1m2_inv_mod_m3)).val();
        ret[i] = (x[i] + 167772161 * v1 + m1m2_mod_1000000007 * v2);
    }
    return ret;
}

struct FPS1000000007 : vector<modint1000000007>{
    using F = FPS1000000007;
    FPS1000000007(const vector<modint1000000007> f) : vector<modint1000000007>(f){}
    FPS1000000007(const int n) : vector<modint1000000007>(n, 0){}

    F operator-() const{
        F res = *this;
        for(auto &e : res) e = -e;
        return res;
    }
    F &operator*=(const modint1000000007 &g){
        for(auto &e : *this) e *= g;
        return *this;
    }
    F &operator/=(const modint1000000007 &g){
        assert(g != modint1000000007(0));
        *this *= g.inv();
        return *this;
    }
    F &operator+=(const F &g){
        int n = (*this).size(), m = g.size();
        int t = min(n, m);
        for(int i = 0; i < t; i++) (*this)[i] += g[i];
        return *this;
    }
    F &operator-=(const F &g){
        int n = (*this).size(), m = g.size();
        int t = min(n, m);
        for(int i = 0; i < t; i++) (*this)[i] -= g[i];
        return *this;
    }

    F &operator*=(const F &g){
        int n = (*this).size();
        *this = convolution1000000007(*this, g);
        (*this).resize(n);
        return *this;
    }
    F operator^(long long d){
        FPS1000000007 ret((*this).size()), f = *this;
        ret[0] = 1;
        while(d > 0) {
            if (d & 1) ret *= f;
            if (d >>= 1) f *= f;
        }
        return ret;
    }
};


int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << std::fixed << std::setprecision(15);

    while(true){
        ll n, d, x; cin >> n >> d >> x;
        if(n == 0) break;
        FPS1000000007 f(n + 1);
        rep(i, min(x-1, n) + 1) f[i] = 1;
        f = (f ^ d);
        cout << f[n].val() << '\n';
    }

    return 0;
}
accuracy: 1
time_ms: 710
memory_kb: 3600
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;
const int INF = 1e9;
const ll inf = 1LL<<60;

template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;


bool solve() {
  ll n, d, x; cin >> n >> d >> x;
  if (n == 0) return false;
  vector<vector<mint>> dp(n+1, vector<mint>(n+1));
  dp[0][0] = mint(1);
  vector<mint> acc(n+1);
  acc[0] = dp[0][0];
  for (int i=1; i<=n; i++) acc[i] = acc[i-1] + dp[0][i];
  for (int i=1; i<=n; i++) {
    for (int j=i; j<=n; j++) dp[i][j] = acc[j-1] - (j-x >= 0 ? acc[j-x] : 0);
    vector<mint> acc_(n+1);
    for (int j=1; j<=n; j++) acc_[j] = acc_[j-1] + dp[i][j];
    swap(acc, acc_);
  }/*
  for (int i=0; i<=n; i++) {
    for (int j=0; j<=n; j++) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }*/
  mint ans = mint(0);
  mint C = mint(1);
  for (int i=1; i<=min(n, d); i++) {
    C *= mint(d-i+1);
    C /= mint(i);
  //  cout << C << endl;
    ans += dp[i][n]*C;
    //cout << ans << endl;
  }
  cout << ans << endl;
  return true;
}

int main() {
  while (solve());
}
accuracy: 1
time_ms: 320
memory_kb: 18812
final_rank: 8

code_snippet: // #pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>

#include <limits>
#include <type_traits>

namespace suisen {
// ! utility
template <typename ...Types>
using constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;
template <bool cond_v, typename Then, typename OrElse>
constexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {
    if constexpr (cond_v) {
        return std::forward<Then>(then);
    } else {
        return std::forward<OrElse>(or_else);
    }
}

// ! function
template <typename ReturnType, typename Callable, typename ...Args>
using is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;
template <typename F, typename T>
using is_uni_op = is_same_as_invoke_result<T, F, T>;
template <typename F, typename T>
using is_bin_op = is_same_as_invoke_result<T, F, T, T>;

template <typename Comparator, typename T>
using is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;

// ! integral
template <typename T, typename = constraints_t<std::is_integral<T>>>
constexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;
template <typename T, unsigned int n>
struct is_nbit { static constexpr bool value = bit_num<T> == n; };
template <typename T, unsigned int n>
static constexpr bool is_nbit_v = is_nbit<T, n>::value;

// ?
template <typename T>
struct safely_multipliable {};
template <>
struct safely_multipliable<int> { using type = long long; };
template <>
struct safely_multipliable<long long> { using type = __int128_t; };
template <>
struct safely_multipliable<float> { using type = float; };
template <>
struct safely_multipliable<double> { using type = double; };
template <>
struct safely_multipliable<long double> { using type = long double; };
template <typename T>
using safely_multipliable_t = typename safely_multipliable<T>::type;

} // namespace suisen

// ! type aliases
using i128 = __int128_t;
using u128 = __uint128_t;
using ll = long long;
using uint = unsigned int;
using ull  = unsigned long long;

template <typename T> using vec  = std::vector<T>;
template <typename T> using vec2 = vec<vec <T>>;
template <typename T> using vec3 = vec<vec2<T>>;
template <typename T> using vec4 = vec<vec3<T>>;

template <typename T>
using pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <typename T, typename U>
using umap = std::unordered_map<T, U>;

// ! macros (capital: internal macro)
#define OVERLOAD2(_1,_2,name,...) name
#define OVERLOAD3(_1,_2,_3,name,...) name
#define OVERLOAD4(_1,_2,_3,_4,name,...) name

#define REP4(i,l,r,s)  for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))
#define REP3(i,l,r)    REP4(i,l,r,1)
#define REP2(i,n)      REP3(i,0,n)
#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))
#define REPINF2(i,l)   REPINF3(i,l,1)
#define REPINF1(i)     REPINF2(i,0)
#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))
#define RREP3(i,l,r)   RREP4(i,l,r,1)
#define RREP2(i,n)     RREP3(i,0,n)

#define rep(...)    OVERLOAD4(__VA_ARGS__, REP4   , REP3   , REP2   )(__VA_ARGS__)
#define rrep(...)   OVERLOAD4(__VA_ARGS__, RREP4  , RREP3  , RREP2  )(__VA_ARGS__)
#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)

#define CAT_I(a, b) a##b
#define CAT(a, b) CAT_I(a, b)
#define UNIQVAR(tag) CAT(tag, __LINE__)
#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)

#define all(iterable) (iterable).begin(), (iterable).end()
#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)

// ! I/O utilities

// pair
template <typename T, typename U>
std::ostream& operator<<(std::ostream& out, const std::pair<T, U> &a) {
    return out << a.first << ' ' << a.second;
}
// tuple
template <unsigned int N = 0, typename ...Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...> &a) {
    if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {
        return out;
    } else {
        out << std::get<N>(a);
        if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {
            out << ' ';
        }
        return operator<<<N + 1>(out, a);
    }
}
// vector
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T> &a) {
    for (auto it = a.begin(); it != a.end();) {
        out << *it;
        if (++it != a.end()) out << ' ';
    }
    return out;
}
inline void print() { std::cout << '\n'; }
template <typename Head, typename... Tail>
inline void print(const Head &head, const Tail &...tails) {
    std::cout << head;
    if (sizeof...(tails)) std::cout << ' ';
    print(tails...);
}
template <typename Iterable>
auto print_all(const Iterable& v, std::string sep = " ", std::string end = "\n") -> decltype(std::cout << *v.begin(), void()) {
    for (auto it = v.begin(); it != v.end();) {
        std::cout << *it;
        if (++it != v.end()) std::cout << sep;
    }
    std::cout << end;
}

// pair
template <typename T, typename U>
std::istream& operator>>(std::istream& in, std::pair<T, U> &a) {
    return in >> a.first >> a.second;
}
// tuple
template <unsigned int N = 0, typename ...Args>
std::istream& operator>>(std::istream& in, std::tuple<Args...> &a) {
    if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {
        return in;
    } else {
        return operator>><N + 1>(in >> std::get<N>(a), a);
    }
}
// vector
template <typename T>
std::istream& operator>>(std::istream& in, std::vector<T> &a) {
    for (auto it = a.begin(); it != a.end(); ++it) in >> *it;
    return in;
}
template <typename ...Args>
void read(Args &...args) {
    ( std::cin >> ... >> args );
}

// ! integral utilities

// Returns pow(-1, n)
template <typename T>
constexpr inline int pow_m1(T n) {
    return -(n & 1) | 1;
}
// Returns pow(-1, n)
template <>
constexpr inline int pow_m1<bool>(bool n) {
    return -int(n) | 1;
}

// Returns floor(x / y)
template <typename T>
constexpr inline T fld(const T x, const T y) {
    return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;
}
template <typename T>
constexpr inline T cld(const T x, const T y) {
    return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;
}

template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcount(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcountll(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }
template <typename T>
constexpr inline int floor_log2(const T x) { return suisen::bit_num<T> - 1 - count_lz(x); }
template <typename T>
constexpr inline int ceil_log2(const T x)  { return floor_log2(x) + ((x & -x) != x); }
template <typename T>
constexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }
template <typename T>
constexpr inline int parity(const T x) { return popcount(x) & 1; }

struct all_subset {
    struct all_subset_iter {
        const int s; int t;
        constexpr all_subset_iter(int s) : s(s), t(s + 1) {}
        constexpr auto operator*() const { return t; }
        constexpr auto operator++() {}
        constexpr auto operator!=(std::nullptr_t) { return t ? (--t &= s, true) : false; }
    };
    int s;
    constexpr all_subset(int s) : s(s) {}
    constexpr auto begin() { return all_subset_iter(s); }
    constexpr auto end()   { return nullptr; }
};

// ! container

template <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>
auto priqueue_comp(const Comparator comparator) {
    return std::priority_queue<T, std::vector<T>, Comparator>(comparator);
}

template <typename Iterable>
auto isize(const Iterable &iterable) -> decltype(int(iterable.size())) {
    return iterable.size();
}

template <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>
auto generate_vector(int n, Gen generator) {
    std::vector<T> v(n);
    for (int i = 0; i < n; ++i) v[i] = generator(i);
    return v;
}

template <typename T>
void sort_unique_erase(std::vector<T> &a) {
    std::sort(a.begin(), a.end());
    a.erase(std::unique(a.begin(), a.end()), a.end());
}

// ! other utilities

// x <- min(x, y). returns true iff `x` has chenged.
template <typename T>
inline bool chmin(T &x, const T &y) {
    if (y >= x) return false;
    x = y;
    return true;
}
// x <- max(x, y). returns true iff `x` has chenged.
template <typename T>
inline bool chmax(T &x, const T &y) {
    if (y <= x) return false;
    x = y;
    return true;
}

namespace suisen {}
using namespace suisen;
using namespace std;

struct io_setup {
    io_setup(int precision = 20) {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(precision);
    }
} io_setup_ {};

// ! code from here

constexpr int P = 1000000007;
constexpr int M = 100000;

int main() {
    vector<long long> invs(M);
    invs[1] = 1;
    rep(i, 2, M) {
        invs[i] = (P - P / i) * invs[P % i] % P;
    }
    auto binom = [&invs](long long n, int r) {
        n %= P;
        long long res = 1;
        rep(d, r) {
            res = res * (n - d) % P;
            res = res * invs[d + 1] % P;
        }
        return res < 0 ? res + P : res;
    };
    while (true) {
        input(int, m);
        input(long long, n);
        input(int, x);
        if (m == 0 and n == 0 and x == 0) break;
        long long ans = 0;
        for (int j = 0; j * x <= m; ++j) {
            int i = m - j * x;
            long long v = binom(i + n - 1, i) * binom(n, j) % P;
            if (j & 1) {
                ans -= v;
            } else {
                ans += v;
            }
        }
        ans %= P;
        print(ans < 0 ? ans + P : ans);
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3752
final_rank: 2