#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)}; } ll N, B, D; ll DINV; ll power(const ll p, const ll n) { if (n == 0) { return 1; } if (n % 2 == 1) { return power(p, n - 1) * p % MOD; } else { const ll pp = power(p, n / 2); return pp * pp % MOD; } } ll stone(ll i) { return (((power(D + 1, i + 2) + MOD - 1) * DINV % MOD) + MOD - (i + 2)) % MOD; } int main() { cin >> N >> B >> D; DINV = power(D, MOD - 2); ll ans = 0; ll rest = N; while (rest > 0) { int ind = 0; ll H = D + 1; for (; H - 1 <= rest; ind++, H *= (D + 1)) { } if (ind == 0) { ans += rest; rest = 0; continue; } 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; } } cout << (stone(B - 1) + MOD - ans) % MOD << endl; return 0; }