Description:
Accept:
#include <bits/stdc++.h>
using namespace std;
typedef int LL;
const LL maxn = 50000001, mod = (1000000000 + 7);
LL n, m, k, a[maxn];
LL P(LL a[], LL p, LL r)
{
LL i = p, j = r + 1, x = a[p];
while (1)
{
while (a[++i] < x && i < r);
while (a[--j] > x && i >= p);
if (j <= i)break;
swap(a[i], a[j]);
}
a[p] = a[j];
a[j] = x;
return j;
}
LL f(LL a[], LL p, LL r, LL k)
{
if (p == r)return a[p];
LL ind = P(a, p, r), j = ind - p + 1;
if (j >= k)
return f(a, p, ind, k);
else
return f(a, ind + 1, r, k - j);
}
int main()
{
cin >> n >> m >> k;
memset(a, 0, sizeof(a));
a[0] = m;
for (LL i = 1; i < n; i++)
a[i] = 1LL*a[i - 1] * m % mod;
cout << f(a, 0, n - 1, k) << endl;
return 0;
}