Peter has a sequence of integers a1, a2, ..., an. Peter wants all numbers in the sequence to equal h. He can perform the operation of "adding one on the segment [l, r]": add one to all elements of the sequence with indices from l to r (inclusive). At that, Peter never chooses any element as the beginning of the segment twice. Similarly, Peter never chooses any element as the end of the segment twice. In other words, for any two segments [l1, r1] and [l2, r2], where Peter added one, the following inequalities hold: l1 ≠ l2 and r1 ≠ r2.
How many distinct ways are there to make all numbers in the sequence equal h? Print this number of ways modulo 1000000007 (109 + 7). Two ways are considered distinct if one of them has a segment that isn't in the other way.
The first line contains two integers n, h (1 ≤ n, h ≤ 2000). The next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 2000).
Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).
3 2 1 1 1
4
5 1 1 1 1 1 1
1
4 3 3 2 1 1
0
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const int MAXN = 2010;
int dp[MAXN][MAXN];
int a[MAXN];
int n,h;
int dfs(int i,int k)
{
if(dp[i][k])return dp[i][k];
if(i==n)return dp[i][k]= k==0 ? 1 : 0;
int res=0;
if(a[i]+k==h)res=((ll)dfs(i+1,k-1)*k % MOD+dfs(i+1,k)) % MOD;
else if(a[i]+k+1==h)res=((ll)dfs(i+1,k)*(k+1) % MOD + dfs(i+1,k+1) ) %MOD;
return dp[i][k]=res;
}
int main()
{
scanf("%d%d",&n,&h);
for(int i=0;i
h){ printf("0\n"); return 0; } } printf("%d\n",dfs(0,0)); return 0; }