For each query bound K, count length-N lists with values 1 to K where each value above 1 has its predecessor before its last occurrence.
Hard8CombinatoricsDynamic programmingMathNo attempts yetTime limit3sMemory limit256 MBJurgen Guntherswarchzhaffenstrassen is known for his guitar playing and for the harsh way he teaches his students. Most people miss one more thing about him. He also likes numbers.
Jurgen has been studying sorted lists lately, and he got bored. Sorted lists are too predictable and there are too few of them, so he changed the rule a little.
Take a list ℓ of N positive integers. The elements do not have to be distinct. Jurgen calls ℓ just a bit sorted if for every integer x>1 that occurs in ℓ, the value x−1 occurs at least once before the last occurrence of x in ℓ. For example:
Jurgen wants to know how many different just a bit sorted lists of N positive integers not greater than K exist. Two lists are different if they differ in at least one position. Count them for him.
The first line contains the length N of the lists and the number Q of queries (1≤N≤5000, 1≤Q≤1000). The second line contains the integers K1,K2,…,KQ. The lists counted in the i-th query contain no value greater than Ki (1≤Ki≤109).
Print one line with Q integers separated by single spaces. The i-th integer is the number of different just a bit sorted lists of N positive integers not greater than Ki. This number can be very large, so print it modulo 109+7.