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
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
#include <unordered_map>
#include <string>

//powers of n for given k
std::unordered_map<std::string, long> n_pow;
std::unordered_map<std::string, long> results;

long mod = 1'000'000'007;

long simple_corner_cases(long k, long n) {
    //a
    if(n == 1) {
        // to be removed ?
        return 0;
    }

    //aa
    if(n == 2) {
        return k;
    }

    //a_a
    if(n == 3) {
        return k * k % mod;
    }

    //a__a + aabb
    if(n==4) {
        return ((k * k % mod) * k % mod) + (k * (k-1) % mod);
    }

    //n=5:  a___a + (a_abb || aab_b)
    return (((k * k % mod) * k % mod) * k % mod) + ((k * k % mod) * (k-1) * 2 % mod);
}

long get_k_pow(long k, long n) {
    if(n == 0) {
        return 1;
    }

    if(n == 1) {
        return k;
    }

    auto key_p = "k" + std::to_string(k) + "n"  + std::to_string(n);
    if(auto found = n_pow.find(key_p); found != n_pow.end()) {
        return found->second;
    }

    long res = 1;
    res *= get_k_pow(k, n-1);
    res %= mod;

    n_pow[key_p] = res;
    return res;
}

long solution(long k, long n) {
    if(n <= 5) {
        return simple_corner_cases(k, n);
    }

    std::string key_r = "k" + std::to_string(k) + "n" + std::to_string(n);
    if(auto found = results.find(key_r); found != results.end()) {
        return found->second;
    }

    //a.............a  -> k^n
    //       +
    //a(.......)a(.....dp.....)b(.......)b
    //  (k-1)^m                  (k-1)^l    ---> 2 * [0..(k-1)^m] * dp(k, n - m) * k * (k-1)

    long res = 0;
    auto key_p = "k" + std::to_string(k) + "n"  + std::to_string(n-2);
    if(auto found = n_pow.find(key_p); found != n_pow.end()) {
        res += k * found->second % mod;
    } else {
        res += k * get_k_pow(k, n-2);
    }
    res %= mod;

    //m=0:      aa     dp    bb
    //m=n-6:    aadpb...n-6...b
    for(int m = 0; m <= n-6; ++m) {
        long part_res = 2;
        //a.....b
        part_res *= k;
        part_res %= mod;
        part_res *= k-1;
        part_res %= mod;

        //_a.....b_
        part_res *= k;
        part_res %= mod;
        part_res *= k-1;
        part_res %= mod;

        //aadpb..k_pow..b
        part_res *= get_k_pow(k-1, m);
        part_res %= mod;

        //dp
        part_res *= solution(k, n-m);
        part_res %= mod;

        res += part_res;
        res %= mod;
    }

    results[key_r] = res;
    return res;
}

int main()
{
    long k, n;
    std::cin >> n >> k;

    if(n == 1) {
        std::cout << 0 << '\n';
    }

    if(k == 1) {
        std::cout << 1 << '\n';
    }

    std::cout << solution(k, n) << '\n';

    return 0;
}