Under construction

Still being built. Things may change

Kaldorian Knights

Time limit2sMemory limit1024 MB

Summary
Count rankings of n knights where the last k_1 + ... + k_l places never hold only knights of houses 1 to l, modulo 1e9+7.
Level

Medium7 of 10

Topics
Combinatorics, Dynamic programming, Math
Solved
No attempts yet

Problem

The king of Kaldoria traditionally celebrates his birthday by inviting the knights of his realm to a big jousting tournament, and every noble house participates by sending their best knights to win fame and glory. At the end of the tournament, the king does not only choose a winner but ranks all nn knights from worst to best.

The number of knights belonging to house ii is denoted by kik_i. Each knight serves at most one house. Some knights may not serve any house. The houses are ordered by their influence in the kingdom, with the first one being the most influential. If the k1k_1 knights of the most powerful house take the last k1k_1 places in the tournament, the house will incite a revolt against the king and crown. The members of the second most influential house are not that powerful. Even if all k2k_2 of its knights end up at the bottom of the ranking, it would be a strong provocation, but the house would not be able to start a revolt. However, if the last k1+k2k_1 + k_2 places are taken by all the knights of the two most influential houses combined, the two houses will unite and fight the king. More generally, if the knights of the ℓ\ell most powerful houses occupy the last k1+k2+⋯+kℓk_1 + k_2 + \dots + k_\ell places in the tournament, they will unite and incite a revolt.

A revolt must be avoided at all cost. The king often chooses the ranking on the spot without much thought, so the chief mathematician of the crown has been asked to count how many rankings will not lead to a revolt.

Input

The first line contains integers nn (1≤n≤1061 \leq n \leq 10^6) and hh (0≤h≤50000 \leq h \leq 5000), the number of knights and the number of houses. Then hh lines follow, where the ii-th line contains an integer kik_i (1≤ki≤n1 \leq k_i \leq n), the number of knights from house ii. Every house has at least one knight. It holds that ∑i=1hki≤n\sum_{i=1}^h k_i \leq n.

Output

Output the number of rankings that do not lead to a revolt, modulo 109+710^9+7, since this number can be quite large.

Examples3

  1. Example 1

    Input
    3 0
    
    Expected output
    6
    
  2. Example 2

    Input
    4 1
    3
    
    Expected output
    18
    
  3. Example 3

    Input
    4 2
    2
    1
    
    Expected output
    16