#abc307e. E - Distinct Adjacent

E - Distinct Adjacent

Score : 475475 points

问题描述

设有 NN 名人员,编号从 11NN,他们站成一个圆圈。其中,编号为 11 的人站在编号为 22 的人的右边,编号为 22 的人站在编号为 33 的人的右边,依此类推,编号为 NN 的人站在编号为 11 的人的右边。

我们将给这 NN 名人员每人分配一个介于 00M1M-1(包含两端点)之间的整数。

MNM^N 种分配整数的方式中,找出满足没有相邻两人分配到相同整数的方法总数,结果对 998244353998244353 取模。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN people numbered from 11 to NN standing in a circle. Person 11 is to the right of person 22, person 22 is to the right of person 33, ..., and person NN is to the right of person 11.

We will give each of the NN people an integer between 00 and M1M-1, inclusive.
Among the MNM^N ways to distribute integers, find the number, modulo 998244353998244353, of such ways that no two adjacent people have the same integer.

Constraints

  • 2N,M1062 \leq N,M \leq 10^6
  • NN and MM are integers.

Input

The input is given from Standard Input in the following format:

NN MM

Output

Print the answer.

Sample Input 1

3 3

Sample Output 1

6

There are six desired ways, where the integers given to persons 1,2,31,2,3 are (0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,0,1),(2,1,0)(0,1,2),(0,2,1),(1,0,2),(1,2,0),(2,0,1),(2,1,0).

Sample Input 2

4 2

Sample Output 2

2

There are two desired ways, where the integers given to persons 1,2,3,41,2,3,4 are (0,1,0,1),(1,0,1,0)(0,1,0,1),(1,0,1,0).

Sample Input 3

987654 456789

Sample Output 3

778634319

Be sure to find the number modulo 998244353998244353.

update @ 2024/3/10 08:41:49