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
#include <cstdio>

long long MOD = 1000000007;

int main()
{
  int n, m;
  scanf("%d %d", &n, &m);
  
  if (n == 1)
  {
    printf("0\n");
    return 0;
  }
  
  if (m == 1)
  {
    printf("1\n");
    return 0;
  }
  
  long long t = 1;
  //if (m == 2)
  {
    for (int i = 2; i < n; i++)
    {
      t = m * t + i - 2;
      t %= MOD;
    }
    t *= m;
    t %= MOD;
    printf("%lld\n", t);
  }
	
  
  	
  return 0;
}