#P14459. [ICPC 2025 Xi'an R] Mystique as Iris

[ICPC 2025 Xi'an R] Mystique as Iris

题目描述

The following two steps, on an array xx consisting of positive integers, are called an operation:

  • Select any two adjacent elements in xx, decrease one of them by 11, and set the other to 00.
  • Remove all zeros from xx.

We call xx mystic\textit{mystic} if and only if it can be transformed into an empty sequence after a finite number of operations (possibly zero).

You are given an array aa consisting of nn integers, as well as an integer mm. Each element of aa is either an integer from 11 to mm or 1-1. Your task is to replace every occurrence of 1-1 in aa with any integer from 11 to mm.

Determine the number of distinct mystic arrays aa that can be obtained after the replacement. Since the answer can be very large, output it modulo 109+710^9 + 7.

输入格式

The first line of the input contains two integers nn and mm (2n1062 \le n \le 10^6, 1m1081 \le m \le 10^8).

The second line of the input contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1aim1\le a_i \le m or ai=1a_i=-1).

输出格式

Output a single integer, representing the number of distinct mystic sequences aa that can be obtained, modulo 109+710^9 + 7.

2 2
-1 -1
3
6 10
-1 -1 -1 -1 1 7
9125

提示

In the first test, the array aa is [1,1][-1, -1]. By replacing both 1-1-s, one possible result is [1,2][1, 2]. In this case, we select the two adjacent numbers: decrease the first by 11 and set the second to 00, obtaining [0,0][0, 0]. After removing all zeros, the sequence becomes empty. Hence, [1,2][1, 2] is a mystic sequence.

Similarly, if we replace the 1-1-s with [1,1][1, 1] or with [2,1][2, 1], both sequences can also be reduced to empty. Therefore, the total number of distinct mystic sequences is 33.