luogu#P5204. [USACO19JAN] Train Tracking 2 P

[USACO19JAN] Train Tracking 2 P

Background

USACO January 2019 Contest, Platinum Problem 3.

Problem Description

Every day, an express train passes by the farm. The train has NN cars (1N1051 \le N \le 10^5). Each car has a positive integer label between 11 and 10910^9; different cars may have the same label.

Normally, Bessie watches the passing train and records the labels on the cars. But today the fog is so thick that Bessie cannot see a single label. Luckily, she learned from a reliable source in the city the minimum values of all sliding windows of the label sequence. Specifically, she is given a positive integer KK, and NK+1N-K+1 positive integers c1,,cN+1Kc_1,\ldots,c_{N+1-K}, where cic_i is the minimum label among cars i,i+1,,i+K1i,i+1,\ldots,i+K-1.

Help Bessie find the number of ways to assign labels to all cars such that all the sliding-window minimum values match. Since this number may be very large, output it modulo 109+710^9+7.

Bessie's information is completely reliable; in other words, it is guaranteed that there exists at least one valid labeling.

Input Format

The first line contains two space-separated integers NN and KK. The remaining lines contain all sliding-window minimum values c1,,cN+1Kc_1,\ldots,c_{N+1-K}, one number per line.

Output Format

Output one integer: the number of ways to assign each car a positive integer label not exceeding 10910^9, modulo 109+710^9+7, such that the minimum label among cars i,i+1,,i+K1i,i+1,\ldots,i+K-1 equals cic_i, for all 1iNK+11 \le i \le N-K+1.

4 2
999999998
999999999
999999998
3

Hint

Translated by ChatGPT 5