Problem: Reordering the Documents

Susan is good at arranging her dining table for convenience, but not her office desk.

Susan has just finished the paperwork on a set of documents, which are still piled on her desk. They have serial numbers and were stacked in order when her boss brought them in. The ordering, however, is not perfect now, as she has been too lazy to put the documents slid out of the pile back to their proper positions. Hearing that she has finished, the boss wants her to return the documents immediately in the document box he is sending her. The documents should be stowed in the box, of course, in the order of their serial numbers.

The desk has room just enough for two more document piles where Susan plans to make two temporary piles. All the documents in the current pile are to be moved one by one from the top to either of the two temporary piles. As making these piles too tall in haste would make them tumble, not too many documents should be placed on them. After moving all the documents to the temporary piles and receiving the document box, documents in the two piles will be moved from their tops, one by one, into the box. Documents should be in reverse order of their serial numbers in the two piles to allow moving them to the box in order.

For example, assume that the pile has six documents #1, #3, #4, #2, #6, and #5, in this order from the top, and that the temporary piles can have no more than three documents. Then, she can form two temporary piles, one with documents #6, #4, and #3, from the top, and the other with #5, #2, and #1 (Figure E.1). Both of the temporary piles are reversely ordered. Then, comparing the serial numbers of documents on top of the two temporary piles, one with the larger number (#6, in this case) is to be removed and stowed into the document box first. Repeating this, all the documents will be perfectly ordered in the document box.

<image>
Figure E.1. Making two temporary piles

Susan is wondering whether the plan is actually feasible with the documents in the current pile and, if so, how many different ways of stacking them to two temporary piles would do. You are asked to help Susan by writing a program to compute the number of different ways, which should be zero if the plan is not feasible.

As each of the documents in the pile can be moved to either of the two temporary piles, for $n$ documents, there are $2^n$ different choice combinations in total, but some of them may disturb the reverse order of the temporary piles and are thus inappropriate.

The example described above corresponds to the first case of the sample input. In this case, the last two documents, #5 and #6, can be swapped their destinations. Also, exchanging the roles of two temporary piles totally will be OK. As any other move sequences would make one of the piles higher than three and/or make them out of order, the total number of different ways of stacking documents to temporary piles in this example is $2 \times 2 = 4$.

Input

The input consists of a single test case of the following format.


$n$ $m$
$s_1$ ...  $s_n$


Here, $n$ is the number of documents in the pile ($1 \leq n \leq 5000$), and $m$ is the number of documents that can be stacked in one temporary pile without committing risks of making it tumble down ($n/2 \leq m \leq n$). Numbers $s_1$ through $s_n$ are the serial numbers of the documents in the document pile, from its top to its bottom. It is guaranteed that all the numbers $1$ through $n$ appear exactly once.

Output

Output a single integer in a line which is the number of ways to form two temporary piles suited for the objective. When no choice will do, the number of ways is $0$, of course.

If the number of possible ways is greater than or equal to $10^9 + 7$, output the number of ways modulo $10^9 + 7$.

Sample Input 1


6 3
1 3 4 2 6 5


Sample Output 1


4


Sample Input 2


6 6
1 3 4 2 6 5


Sample Output 2


8


Sample Input 3


4 4
4 3 1 2


Sample Output 3


0






Example

Input

6 3
1 3 4 2 6 5


Output

4

Solution (in CPP):

Solution:
#include<bits/stdc++.h>

using namespace std;

#define REP(i,n) for(ll (i) = 0;(i) < (n);i++)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) b.begin(),v.end()
#define SHOW1d(v, n) {REP(WW,n)cerr<<v[WW]<<" ";cerr<<endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr<<v[W_][H_]<<" ";cerr<<endl;}cerr<<endl;}
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

struct UF
{
  vector<int> par;
  vector<int>sz;
  vector<int>rank;
  UF(int n):par(n),sz(n),rank(n){
    for(int i=0;i<n;i++){
      par[i] = i;sz[i] = 1;rank[i] = 0;
    }
  }
  int find(int x){
    if(par[x]==x)return x;
    else return par[x] = find(par[x]);
  }
  void unite(int x, int y){
    x = find(x);y = find(y);
    if(x == y)return;
    if(rank[x] < rank[y]){
      par[x] = y;
      sz[y] += sz[x];
    }else{
      par[y] = x;
      sz[x] += sz[y];
      if(rank[x] == rank[y])rank[x]++;
    }
  }
  bool same(int x,int y){return find(x) == find(y);}
  int size(int n){return sz[find(n)];}
};

vector<vector<int>> g(5555);
bool used[5555];
int color[5555];
int color_a, color_b;
int dp[5555][5555];
bool ng_flag;

void mod_add(int &a, int b){
  a += b;
  a %= MOD;
}

void dfs(int a){
  //cout << "dfs in " << a << endl;
  used[a] = true;
  if(color[a] == 1)color_a++;
  else color_b++;

  REP(i, g[a].size()){
    //cout << "dfs " << a << " " << g[a][i] << endl;
    int aite = g[a][i];
    if(color[aite] * color[a] == 1){
      //cout << "!! " << aite << " " << a << endl;
      ng_flag = true;
      continue;
    }
    if(used[aite])continue;
    if(color[aite] == 0){
      color[aite] = -color[a];
    }
    dfs(aite);
  }
}

int main() {
  cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);

  ll n, m;cin >> n >> m;
  vector<ll> v(n);
  REP(i, n)cin>>v[i];

  for(int i = n-1;i >= 0;i--){
    for(int j = i-1;j >= 0;j--){
      if(v[i] < v[j]){
        //cout << "! " << v[i] << " " << v[j] << endl;
        g[i].PB(j);
        g[j].PB(i);
      }
    }
  }
  vector<P> seica;

  REP(i, n){
    //cout << i << endl;
    if(used[i])continue;
    color[i] = 1;
    color_a = 0;
    color_b = 0;
    dfs(i);
    if(ng_flag){
      cout << 0 << endl;
      return 0;
    }
    seica.EB(color_a, color_b);
  }

/*
  SHOW1d(color, n);
  REP(i, seica.size()){
    cout << "(" << seica[i].FI << " " << seica[i].SE << ")";
  }cout << endl;
*/
  dp[0][0] = 1;
  int sum = 0;
  REP(i, seica.size()){
    REP(j, 5555){
      if(dp[i][j] == 0)continue;
      ll a_high = j;
      ll b_high = sum - j;
      //cout << i << " " << a_high << " " << b_high << endl;
      if(a_high + seica[i].FI <= m && b_high + seica[i].SE <= m){
        //cout << "A" << endl;
        mod_add(dp[i+1][a_high + seica[i].FI], dp[i][j]);
      }
      if(a_high + seica[i].SE <= m && b_high + seica[i].FI <= m){
        //cout << "B" << endl;
        mod_add(dp[i+1][a_high + seica[i].SE], dp[i][j]);
      }
    }
    sum += seica[i].FI;
    sum += seica[i].SE;
  }

  //SHOW2d(dp, seica.size() + 1, 10);

  int ans = 0;
  REP(i, 5555)mod_add(ans, dp[seica.size()][i]);
  cout << ans << endl;

  return 0;
}