CF BUDDY
← Problems·

1227F2 · Wrong Answer on test 233 (Hard Version)

2400 · combinatorics, math

Problem: This is the harder version of the problem. In this version, 1n21051 \le n \le 2\cdot10^5. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems.

The problem is to finish nn one-choice-questions. Each of the questions contains kk options, and only one of them is correct. The answer to the ii-th question is hih_{i}, and if your answer of the question ii is hih_{i}, you earn 11 point, otherwise, you earn 00 points for this question. The values h1,h2,,hnh_1, h_2, \dots, h_n are known to you in this problem.

However, you have a mistake in your program. It moves the answer clockwise! Consider all the nn answers are written in a circle. Due to the mistake in your program, they are shifted by one cyclically.

Formally, the mistake moves the answer for the question ii to the question imodn+1i \bmod n + 1. So it moves the answer for the question 11 to question 22, the answer for the question 22 to the question 33, ..., the answer for the question nn to the question 11.

We call all the nn answers together an answer suit. There are knk^n possible answer suits in total.

You're wondering, how many answer suits satisfy the following condition: after moving clockwise by 11, the total number of points of the new answer suit is strictly larger than the number of points of the old one. You need to find the answer modulo 998244353998\,244\,353.

For example, if n=5n = 5, and your answer suit is a=[1,2,3,4,5]a=[1,2,3,4,5], it will submitted as a=[5,1,2,3,4]a'=[5,1,2,3,4] because of a mistake. If the correct answer suit is h=[5,2,2,3,4]h=[5,2,2,3,4], the answer suit aa earns 11 point and the answer suite aa' earns 44 points. Since 4>14 > 1, the answer suit a=[1,2,3,4,5]a=[1,2,3,4,5] should be counted.

Input Format: The first line contains two integers nn, kk (1n21051 \le n \le 2\cdot10^5, 1k1091 \le k \le 10^9) — the number of questions and the number of possible answers to each question.

The following line contains nn integers h1,h2,,hnh_1, h_2, \dots, h_n, (1hik)1 \le h_{i} \le k) — answers to the questions.

Output Format: Output one integer: the number of answers suits satisfying the given condition, modulo 998244353998\,244\,353.

Note: For the first example, valid answer suits are [2,1,1],[2,1,2],[2,1,3],[3,1,1],[3,1,2],[3,1,3],[3,2,1],[3,2,2],[3,2,3][2,1,1], [2,1,2], [2,1,3], [3,1,1], [3,1,2], [3,1,3], [3,2,1], [3,2,2], [3,2,3].

Sample Cases

Case 1

Input

3 3
1 3 1

Output

9

Case 2

Input

5 5
1 1 4 2 2

Output

1000

Case 3

Input

6 2
1 1 2 2 1 1

Output

16

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback