Count of Array

 You love array, don't you?

You are given two integer N and M, along with an array B of length N containing pairwise distinct integers. You have to find the number of array A of length N satisfying the follow conditions:

  1. 1≤Ai≤M.
  2. Ai≠Aj for all 1≤i<j≤N.
  3. Ai≠Bi for all 1≤i≤N.

Since the answer can be very large, output it under modulo 109+7.

Input Format

  • The first line of the input contains two space-separated integers N and M.
  • The second line of the input contains N separated integers B1,B2,…,BN - the array B.

Output Format

Output on a single line the number of satisfactory array modulo 109+7.

Constraints

  • 1≤N≤2⋅105
  • 1≤M≤3⋅105
  • 1≤Bi≤109
  • Bi≠Bj for all 1≤i<j≤N.

Sample Input 1 

3 4
5 1 2

Sample Output 1 

14

Explanation

All satisfactory arrays are:

  • [1,2,3]
  • [2,3,1]
  • [3,2,1]
  • [1,2,4]
  • [2,4,1]
  • [4,2,1]
  • [1,3,4]
  • [1,4,3]
  • [3,4,1]
  • [4,3,1]
  • [2,3,4]
  • [2,4,3]
  • [3,2,4]
  • [4,2,3]

Sample Input 2 

2 2
1 3

Sample Output 2 

1

Explanation

The only satisfactory array is:

Comments

Popular posts from this blog

Flipkart Runway: Season 3 | 2025 | Internship

Yet Another Tree Problem

Global Alphathon | Internship OR Full-Time | Apply Now

Software Testing Week 5 : Assignment 5 | 2022