Counting Permutations with a Given Greedily Increasing Subsequence

Time limit1sMemory limit512 MB

Summary
Count the permutations of 1 to N whose greedily increasing subsequence equals a given sequence G, modulo 1e9+7.
Level

Hard8 of 10

Topics
Combinatorics, Math, Dynamic programming, Implementation
Solved
No attempts yet

Problem

Given a permutation A=(a1,a2,…,aN)A = (a_1, a_2, \dots, a_N) of the integers 1,2,…,N1, 2, \dots, N, we define the greedily increasing subsequence (GIS) in the following way.

Let g1=a1g_1 = a_1. For every i>1i > 1, let gig_i be the leftmost integer in AA that is strictly larger than gi−1g_{i-1}. If for a given ii there is no such integer, we say that the GIS of the sequence is the sequence (g1,g2,...,gi−1)(g_1, g_2, ..., g_{i - 1}).

For example, consider the permutation (2,3,1,5,4,7,6)(2, 3, 1, 5, 4, 7, 6). First, we have g1=2g_1 = 2. The leftmost integer larger than 22 is 33, so g2=3g_2 = 3. The leftmost integer larger than 33 is 55 (11 is too small), so g3=5g_3 = 5. Finally, g4=7g_4 = 7. Thus, the GIS of (2,3,1,5,4,7,6)(2, 3, 1, 5, 4, 7, 6) is (2,3,5,7)(2, 3, 5, 7).

Given a sequence G=(g1,g2,…,gL)G = (g_1, g_2, \dots, g_L), how many permutations AA of the integers 1,2,…,N1, 2, \dots, N have GG as its GIS?

Input

The first line of input contains the integers 1≤N≤1061 \le N \le 10^6, the number of elements of the permutation AA, and 1≤L≤1061 \le L \le 10^6, the length of the sequence GG.

The next line contains LL positive integers between 11 and NN, the elements g1,…,gLg_1, \dots, g_L of the sequence GG.

Output

Output a single integer: the number of NN-element permutations having the given sequence as its GIS. Since this number may be large, output it modulo the prime number 109+710^9 + 7.

Examples4

  1. Example 1

    Input
    5 1
    1
    
    Expected output
    0
    
  2. Example 2

    Input
    5 1
    5
    
    Expected output
    24
    
  3. Example 3

    Input
    5 3
    2 4 5
    
    Expected output
    8
    
  4. Example 4

    Input
    7 4
    1 4 5 7
    
    Expected output
    20