#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; // a,b の最大公約数と、ax + by = gcd(a,b) となる x,y を求める long long extgcd(long long a, long long b, long long &x, long long &y) { long long g = a; if(b != 0){ g = extgcd(b, a % b, y, x); y -= (a / b) * x; }else{ x = 1; y = 0; } return g; } // ax ≡ gcd(a, m) (mod m) となる x を求める // a, m が互いに素ならば、関数値は mod m での a の逆数となる long long mod_inverse(long long a, long long m) { long long x, y; extgcd(a, m, x, y); return (x % m + m) % m; } // 階乗数の事前計算を用いた各種組合せ class FactorialCalculation { private: const int mod; vector factorial; vector invFactorial; public: FactorialCalculation(int n, int mod) : mod(mod) { factorial.resize(n+1, 1); invFactorial.resize(n+1, 1); for(int i=1; i<=n; ++i){ factorial[i] = factorial[i-1] * i % mod; invFactorial[i] = mod_inverse(factorial[i], mod); } } long long getFactorial(int n){ return factorial[n]; } long long getPermutation(int n, int r){ if(n < r) return 0; return factorial[n] * invFactorial[n-r] % mod; } long long getCombination(int n, int r){ if(n < r) return 0; return getPermutation(n, r) * invFactorial[r] % mod; } long long getHomogeneous(int n, int r){ return getCombination(n+r-1, r); } }; const int MOD = 1000000007; // 累乗、べき乗 long long power(int a, int b) { long long ret = 1; long long tmp = a; while(b > 0){ if(b & 1){ ret *= tmp; ret %= MOD; } tmp *= tmp; tmp %= MOD; b >>= 1; } return ret; } int main() { int n, k, d; cin >> n >> k >> d; int cnt = (n - 1) / (k - 1); int len = n - cnt * (k - 1); if(d == 1){ cout << len << endl; return 0; } if(len == 1){ cout << power(d, cnt) << endl; return 0; } FactorialCalculation calc(len+cnt, MOD); long long ans = 0; for(int i=0; i<=cnt; ++i){ ans += power(d, i) * calc.getHomogeneous(len - 1, cnt - i); ans %= MOD; } ans *= len; ans %= MOD; cout << ans << endl; return 0; }