#include #define show(x) cerr << #x << " = " << x << endl using namespace std; using ll = long long; using ld = long double; constexpr ll MOD = 1000000007LL; template constexpr T INF = numeric_limits::max() / 10; template struct fix_type { Functor functor; template decltype(auto) operator()(Args&&... args) const& { return functor(functor, std::forward(args)...); } }; template fix_type::type> fix(Functor&& functor) { return {std::forward(functor)}; } int main() { ll N, B, D; cin >> N >> B >> D; vector stone(B); stone[0] = D; for (int i = 1; i < B; i++) { stone[i] = (stone[i - 1] * (D + 1) % MOD + D * (i + 1) % MOD) % MOD; } ll ans = 0; ll rest = N; auto rec = fix([&](auto&& self) { int ind = 0; ll H = D + 1; for (; H - 1 <= rest; ind++, H *= (D + 1)) { } if (ind == 0) { ans += rest; return; } const ll h = H / (D + 1); const ll sub = (stone[ind - 1] + (ind + 1)) % MOD; const ll rep = rest / h; rest = rest % h; (ans += rep * sub % MOD) %= MOD; if (rest == h - 1) { (ans += stone[ind - 1]) %= MOD; rest = 0; } self(self); }); rec(); cout << (stone[B - 1] + MOD - ans) % MOD << endl; return 0; }