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
#include <iostream>
#include <vector>
#include <set>

using namespace std;

void C() {
    vector<int> data(121,0);
    int n, k, a;
    int kUsed = 0;

    cin >> n >> k;
    
    for (int i = 0; i < n; i++){
        cin >> a;
        data[a]++;
    }

    int i = 120;

    while (k > kUsed) {
        kUsed += data[i];
        i--;
        if (i < 0)break;
    }
    cout << kUsed;
}

void B() {
    int n, k;
    cin >> n >> k;
    vector<int> bottles(n);
    set<int> usedB;

    for (int i = 0; i < n; i++){
        cin >> bottles[i];
    }
    int bottlesPlaced = 0;
    int a;
    int time = 0;
    for (int i = 0; i < n; i++){
        a = bottles[i];
        if (usedB.find(a) == usedB.end()) {
            time += (i - bottlesPlaced);
            bottlesPlaced++;
            usedB.insert(a);
        }
        if (bottlesPlaced == k) {
            cout << time;
            return;
        }
    }

    cout << "-1";
}

long long Power(long long n, long long p) {
    long long mod = 1000000007;
    if (p == 0) return 1;
    if (p == 1) return n % mod;

    long long a = Power(n, p / 2);
    a *= a;
    a %= mod;

    if (p % 2 == 1) {
        a *= n;
        a %= mod;
    }

    return a;
}

long long Cases(long long n, long long m, vector<long long>& answers) {
    if (n == 0) return 1;
    if (n == 1) return 0;
    long long mod = 1000000007;

    long long ans = Power(m, n-1); //Swój pow potrezbuje mod mod;
    
    if (n <= 3) {
        ans %= mod;
        return ans;
    }
    
    long long k = 1;
    long long a;
    long long tempAns = 0;

    /*for (int i = 1; i < n-2; i++){
        for (int j = i + 1; j < n-1; j++){
            //k *= Power(m - 1, i - 1);
            //k %= mod;
            k *= Power(m - 1, n + i - j - 3);
            k %= mod;
            a = answers[j - i - 1];
            if (a == -1) {
                a = Cases(j - i - 1, m, answers);
                answers[j - i - 1] = a;
            }
            k *= a;
            k %= mod;

            tempAns += k;
            tempAns %= mod;
            k = 1;
        }
    }*/
    long long l;
    for (int i = 0; i <= n-4; i++){
        k = n - 4 - i;
        l = k + 1;
        l *= Power(m - 1, k);
        l %= mod;

        a = answers[i];
        if (a == -1) {
            a = Cases(i, m, answers);
            answers[i] = a;
        }
        l *= a;
        l %= mod;
        tempAns += l;
        tempAns %= mod;
    }

    tempAns *= m;
    tempAns %= mod;
    tempAns *= (m - 1);
    tempAns %= mod;
    ans += tempAns;
    ans %= mod;
    return ans;
}

void A() {
    int n, m;
    cin >> n >> m;
    vector<long long> answers(n + 1, -1);

    
    cout << Cases(n, m, answers);

    //for (int i = 0; i < n + 1; i++){
    //    cout << i << ": " << answers[i]<<'\n';
    //}
}

int main()
{
    A();
    return 0;
}