#P5417. [CTSC2016] 萨菲克斯·阿瑞

[CTSC2016] 萨菲克斯·阿瑞

Description

Xiao P came to the contest site of NOIP2044. He found that the second problem was like this: given a string of length nn, formed by at most mm different characters, where the number of occurrences of the ii-th character is at most cic_i, ask what the suffix array of this string is.

Clever Xiao P thought of a new problem and hopes you can help solve it: under the constraints given in the problem, how many different answers can there be. That is, among all strings of length nn formed by mm characters, where the number of occurrences of the ii-th character is at most cic_i, how many different suffix arrays are there in total.

Since the answer is very large, you only need to output the answer modulo 109+710^9+7.

For a string s=s1s2...sns=s_1s_2...s_n, let suf(i)\mathrm{suf}(i) denote the substring from position ii to the end. The suffix array is a permutation p1,p2,...,pnp_1,p_2,...,p_n of 11 to nn, satisfying $\mathrm{suf}(p_1) < \mathrm{suf}(p_2) < \dots < \mathrm{suf}(p_n)$. For two strings ss and tt, let ii be the first position such that sitis_i \neq t_i. Then the string with the smaller one among sis_i and tit_i is smaller. If such ii does not exist, then the shorter string is smaller.

For the ordering between characters, we define that the 11-st character is the smallest, the 22-nd character is the next smallest, and so on.

Input Format

The first line contains two positive integers n,mn,m, meaning the length of the string is nn, and there are mm kinds of characters.

The next line contains mm non-negative integers c1,c2,...,cmc_1,c_2,...,c_m, meaning the maximum number of occurrences of each character. It is guaranteed that 0c1,c2,...,cmn0 \leq c_1,c_2,...,c_m \leq n and c1+c2+...+cmnc_1+c_2+...+c_m \geq n.

Output Format

Output one line containing a positive integer, representing the answer.

3 2
2 2
5
10 5
2 3 4 3 2

1003811

Hint

Explanation of Sample 1

Let aa be the first kind of character and bb be the second kind of character. Then there are 66 possible strings: aab,aba,abb,baa,bab,bbaaab,aba,abb,baa,bab,bba. Their suffix arrays are

(1,2,3),(3,1,2),(1,3,2),(3,2,1),(2,3,1),(3,2,1)(1,2,3),(3,1,2),(1,3,2),(3,2,1),(2,3,1),(3,2,1)

So there are 55 different results.

Constraints

For the first 5%5\% of the test points, n,m6n, m \leq 6.

Another 10%10\% of the test points: n,m10n, m \leq 10.

Another 20%20\% of the test points: n,m500n, m \leq 500. Among them, for 5%5\% of the test points m=2m = 2, for 5%5\% of the test points m=3m = 3, and for 10%10\% of the test points c1=c2==cm=nc_1 = c_2 = \cdots = c_m = n.

Another 15%15\% of the test points: n,m50n,m \leq 50.

Another 20%20\% of the test points: n,m200n, m \leq 200.

Another 30%30\% of the test points: n,m500n, m \leq 500.

Translated by ChatGPT 5