1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// clang-format off
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(auto i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
auto operator<<(ostream&o,auto p)->decltype(p.first,o){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(ostream&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
// clang-format on

struct Num
{
#ifdef RATIONAL
    LL num, denom;
    Num &simplify();
#else
    int val;
#endif

    Num(int x = 0);
    Num inv() const;
    bool operator==(const Num &oth) const;
};
Num add(const Num &a, const Num &b);
Num sub(const Num &a, const Num &b);
Num mul(const Num &a, const Num &b);
ostream &operator<<(ostream &o, const Num &x);
Num fast_pow(Num base, int exp);

int N, K, M;
vector<Num> exact;
vector<Num> exceeds;
vector<Num> dp1;
vector<Num> dp2;

int
main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> N >> K >> M;
    Num Kinv = Num(K).inv();

    exact.resize(M + 1);
    exact[0] = 1;
    Num window_sum = exact[0];
    FOR (x, 1, ssize(exact) - 1) {
        auto &val = exact[x];
        val = mul(window_sum, Kinv);
        window_sum = add(window_sum, val);
        if (x - K >= 0) window_sum = sub(window_sum, exact[x - K]);
    }
    debug(exact);

    exceeds.resize(M + 1);
    FOR (x, 0, ssize(exceeds) - 1) {
        if (x < M - K) continue;
        exceeds[x] = mul(Num(K - (M - x) + 1), Kinv);
    }
    debug(exceeds);

    dp1.resize(M + 1);
    for (int x = M - 1; x >= 0; --x) {
        dp1[x] = add(dp1[x + 1], mul(exact[x], exceeds[x]));
    }
    debug(dp1);

    dp2.resize(M);
    FOR (x, 0, ssize(dp2) - 1) {
        auto &val = dp2[x];
        if (exceeds[x] == Num(0)) {
            val = mul(Num(N), exact[x]);
            val = mul(val, fast_pow(dp1[x], N - 1));
            continue;
        }
        val = sub(fast_pow(dp1[x], N), fast_pow(dp1[x + 1], N));
        val = mul(val, exceeds[x].inv());
    }
    debug(dp2);

    Num score;
    FOR (x, 0, M - 1) score = add(score, dp2[x]);
    cout << score << "\n";
    return 0;
}

#ifdef RATIONAL
Num::Num(int x)
{
    num = x, denom = 1;
}

Num &
Num::simplify()
{
    LL g = __gcd(num, denom);
    if (g < 0) g = -g;
    num /= g, denom /= g;
    return *this;
}

Num
add(const Num &a, const Num &b)
{
    Num res;
    res.num   = 1LL * a.num * b.denom + 1LL * b.num * a.denom;
    res.denom = 1LL * a.denom * b.denom;
    return res.simplify();
}
Num
sub(const Num &a, const Num &b)
{
    Num mb;
    mb.num = -b.num, mb.denom = b.denom;
    return add(a, mb);
}
Num
mul(const Num &a, const Num &b)
{
    Num res;
    res.num   = a.num * b.num;
    res.denom = a.denom * b.denom;
    return res.simplify();
}
Num
Num::inv() const
{
    Num res;
    res.num = denom, res.denom = num;
    if (num < 0) res.num = -res.num, res.denom = -res.denom;
    return res;
}
ostream &
operator<<(ostream &o, const Num &x)
{
    return o << x.num << "/" << x.denom;
}
bool
Num::operator==(const Num &oth) const
{
    return num * oth.denom == oth.num * denom;
}
#else
const int MOD = 1000000000 + 7;
Num::Num(int x)
{
    while (x < 0) x += MOD;
    val = x % MOD;
}
Num
add(const Num &a, const Num &b)
{
    Num res;
    res.val = a.val + b.val;
    if (res.val >= MOD) res.val -= MOD;
    return res;
}
Num
sub(const Num &a, const Num &b)
{
    Num res = a;
    if (a.val < b.val) res.val += MOD;
    res.val -= b.val;
    return res;
}
Num
mul(const Num &a, const Num &b)
{
    Num res;
    res.val = 1LL * a.val * b.val % MOD;
    return res;
}
Num
Num::inv() const
{
    return fast_pow(*this, MOD - 2);
}
ostream &
operator<<(ostream &o, const Num &x)
{
    return o << x.val;
}
bool
Num::operator==(const Num &oth) const
{
    return val == oth.val;
}
#endif

Num
fast_pow(Num base, int exp)
{
    Num res = 1;
    while (exp) {
        if (exp & 1) res = mul(res, base);
        base = mul(base, base);
        exp >>= 1;
    }
    return res;
}