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.
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
...(truncated)

Submitted solutions:

code_snippet: #include<bits/extc++.h>
#define rep(i,n) for(int i = 0; i< (n); i++)
#define all(a) begin(a),end(a)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int MOD = 1000000007;
const int inf = (1<<30);
const ll INF = (1ull<<62);
const int dx[8] = {1,0,-1,0,1,1,-1,-1};
const int dy[8] = {0,1,0,-1,1,-1,1,-1};
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b){ return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b){ return a > b && (a = b, true);}

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout.setf(ios_base::fixed);
    cout.precision(12);
    int n, m;
    cin >> n >> m;
    vector<int> s(n);
    for (int i = 0; i < n; i++) cin >> s[i];
    vector<pair<int, int>> P;
    vector<int> d(n, -1);
    for (int i = 0; i < n; i++){
        if (d[i] != -1) continue;
        vector<int> A;
        queue<int> q;
        q.push(i);
        d[i] = 0;
        while (!q.empty()){
            int j = q.front();
            q.pop();
            A.push_back(j);
            for (int k = 0; k < n; k++){
                if ((k < j and s[k] > s[j]) or (j < k and s[j] > s[k])){
                    if (d[k] == -1){
                        d[k] = d[j] + 1;
                        q.push(k);
                    }
                }
            }
        }
        for (int i = 0; i < A.size(); i++){
            for (int j = 0; j < A.size(); j++){
                if (A[i] < A[j] and s[A[i]] > s[A[j]]){
                    if (d[A[i]] % 2 == d[A[j]] % 2){
                        cout << 0 << endl;
                        return 0;
                    }
                }
            }
        }
        int cnt = 0;
        for (int i : A) if (d[i] % 2 == 0) cnt++;
        P.push_back({cnt, A.size() - cnt});
    }
    vector<vector<long long>> dp(P.size() + 1, vector<long long>(n + 1));
    dp[0][0] = 1;
    for (int i = 0; i < P.size(); i++){
        auto [l, r] = P[i];
        for (int j = 0; j <= n; j++){
            if (j + l <= n){
                dp[i + 1][j + l] = (dp[i + 1][j + l] + dp[i][j]) % MOD;
            }
            if (j + r <= n){
                dp[i + 1][j + r] = (dp[i + 1][j + r] + dp[i][j]) % MOD;
            }
        }
    }
    long long ans = 0;
    for (int i = n - m; i <= m; i++){
        ans = (ans + dp[P.size()][i]) % MOD;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 150
memory_kb: 198516
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = unsigned int;
#define rep(i, n) for(ll i = 0; i < n; ++i)
#define loop(i,m, n) for(ll i = m; i <= n; ++i)
#define vl vector<ll>
#define vvl vector<vl>
#define mod 1000000007

int main(){
    ll n,m;
	cin>>n>>m;
	vl a(n);
	rep(i,n)cin>>a[i];
	
	set<ll> st;
	loop(i,1,n)st.insert(i);
	ll mx=0;

	//[残りのMIN]>[使った奴のMAX]なら2通り
	//[残りのMIN]<[使った奴のMAX]なら以下の場合分け
	//[使う奴]==[残りのMIN]
	//上じゃないのに[使う奴]<[使った奴のMAX]答えを0通りにして終わり
	//その他

	//dp[i][j][k]=i番目の数字で、左がj個の高さでkがどっちが最大か
	vvl dp(1,vl(2,0));
	dp[0][0]=1;
	//dp[0][0][1]=1;

	rep(i,n){
		vvl ndp=vvl(i+2,vl(2,0));
		rep(j,i+1){
			if(*st.begin()>mx){
				//[残りのMIN]>[使った奴のMAX]なら左右2通り
				
				//左に置く場合
				ndp[j+1][0]+=dp[j][0]+dp[j][1];

				//右に置く場合
				ndp[j][1]+=dp[j][0]+dp[j][1];
			}else if(a[i]==*st.begin()){
				//[使う奴]==[残りのMIN](使う奴は最大でない)

				//左に置く場合
				ndp[j+1][1]+=dp[j][1];

				//右に置く場合
				ndp[j][0]+=dp[j][0];
			}else if(mx<a[i]){
				//[使った奴のMAX]<[使う奴](最小値は残さなきゃいけない)

				//左に置く場合
				ndp[j+1][0]+=dp[j][0];

				//右に置く場合
				ndp[j][1]+=dp[j][1];
			}else{
				//[残りのMIN]<[使う奴]<[使った奴のMAX]答えを0通りにして終わり
				cout<<0<<endl;
				return 0;
			}
			
			ndp[j][0]%=mod;
			ndp[j][1]%=mod;
			ndp[j+1][0]%=mod;
			ndp[j+1][1]%=mod;
		}
		st.erase(a[i]);
		mx=max(a[i],mx);
		swap(dp,ndp);
	}
	ll ans=0;
	rep(i,n+1){
		if(max(i,n-i)>m)continue;
		ans+=dp[i][0];
		ans+=dp[i][1];
	}
	ans%=mod;
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 480
memory_kb: 4092
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;


using ll = long long;
ll modinv(ll a, ll m) {
	assert(m > 0);
	if (m == 1) return 0;
	a %= m;
	if (a < 0) a += m;
	assert(a != 0);
	if (a == 1) return 1;
	return m - modinv(m, a) * m / a;
}

template <int MOD_> struct modnum {
private:
	int v;
public:
	static const int MOD = MOD_;

	modnum() : v(0) {}
	modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
	explicit operator int () const { return v; }
	friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
	friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }

	modnum operator ~ () const {
		modnum res;
		res.v = modinv(v, MOD);
		return res;
	}

	modnum& operator += (const modnum& o) {
		v += o.v;
		if (v >= MOD) v -= MOD;
		return *this;
	}
	modnum& operator -= (const modnum& o) {
		v -= o.v;
		if (v < 0) v += MOD;
		return *this;
	}
	modnum& operator *= (const modnum& o) {
		v = int(ll(v) * ll(o.v) % MOD);
		return *this;
	}
	modnum& operator /= (const modnum& o) {
		return *this *= (~o);
	}

	friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
	friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
	friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
	friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
};

using num = modnum<int(1e9) + 7>;

int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n, m;
	cin >> n >> m;
	vector<int> a(n);
	for(int i = 0; i < n; i++){
		cin >> a[i];
		a[i]--;
	}
	vector<pair<int,int> > sums;
	int cur = -1;
	int cmax = 0;
	for(int i = 0; i < n; i++){
		cmax = max(a[i], cmax);
		if(cmax == i){
			int y = -1;
			int z = -1;
			int cy = 0;
			int cz = 0;
			for(int v = cur + 1; v <= i; v++){
				if(a[v] >= z){
					z = a[v];
					cz++;
				} else if(a[v] >= y){
					y = a[v];
					cy++;
				} else {
					cout << 0 << '\n';
					return 0;
				}
			}
			sums.push_back({cy, cz});
			cur = i;
		}
	}
	vector<num> dp(n+1, 0);
	dp[0] = 1;
	for(pair<int,int> v : sums){
		vector<num> newdp(n+1, 0);
		for(int i = 0; i + v.first <= n; i++) newdp[i+v.first] += dp[i];
		for(int i = 0; i + v.second <= n; i++) newdp[i+v.second] += dp[i];
		dp = newdp;
	}
	num ans = 0;
	for(int i = 0; i <= n; i++){
		if(i <= m && n-i <= m) ans += dp[i];
	}
	cout << (int)ans << '\n';
}
accuracy: 1
time_ms: 40
memory_kb: 3524
final_rank: 3

code_snippet: #define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int _=5e3+5,mod=1e9+7;
int n,m,k=1,maxn,ans;
int dp[_][_];
bool b[_];
void add(int& x,int y){
	(x+=y)%=mod;
}
signed main(){
	scanf("%lld%lld",&n,&m),dp[1][1]=2;
	for(int i=1,x;i<=n;i++){
		scanf("%lld",&x);
		if(x>maxn){
			for(int j=1;j<=i;j++)
				add(dp[i][j],dp[i-1][j-1]);
			if(maxn<k){
				for(int j=1;j<=i;j++)
					add(dp[i][i-j+1],dp[i-1][j-1]);
			}
		}
		if(x==k&&x<maxn){
			for(int j=1;j<=i;j++)
				add(dp[i][j-1],dp[i-1][j-1]);
		}
		b[x]=1,maxn=max(maxn,x);
		while(b[k])
			k++;
	}
	for(int i=n-m;i<=m;i++)
		add(ans,dp[n][i]);
	printf("%lld\n",ans);
}
accuracy: 1
time_ms: 20
memory_kb: 197292
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e3+10,MOD=1e9+7;
int n,m,s[N],dp[N][N],ans;
bool vis[N];
signed main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>n>>m;
    dp[0][0]=1;
    int l=1,maxn=0;
    for(int i=0,s;i<n;i++){
        cin>>s;
        vis[s]=1;
        int now=l;
        while(vis[l])++l;
        if(s>maxn){
            for(int j=0;j<m;j++){
                dp[i+1][j+1]=(dp[i+1][j+1]+dp[i][j])%MOD;
            }
            if(l>maxn){
                for(int j=0;j<=m;j++){
                    dp[i+1][i-j+1]=(dp[i+1][i-j+1]+dp[i][j])%MOD;
                }
            }
        }else if(s==now){
            for(int j=0;j<=m;j++){
                dp[i+1][j]=dp[i][j]%MOD;
            }
        }
        maxn=max(maxn,s);
    }
    for(int i=n-m;i<=m;i++)ans=(ans+dp[n][i])%MOD;
    cout<<ans%MOD<<"\n";
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 199108
final_rank: 15

code_snippet: // AOJ #1404
// Reordering the Documents 2025.2.14

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;

bitset<5000> g[5000];

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

    int n,m;
    cin >> n >> m;
    vector<int> s(n);
    for(int i=0; i<n; i++) cin >> s[i];

    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            if(s[i] > s[j]){
                g[i].set(j);
                g[j].set(i);
            }
        }
    }

    vector<int> color(n, -1);
    vector<pair<int,int>> comps;

    for(int start=0; start<n; start++){
        if(color[start] != -1) continue;
        int c0=0, c1=0;
        queue<int>q;
        color[start] = 0; 
        c0++;
        q.push(start);

        bool conflict = false;

        while(!q.empty() && !conflict){
            int u = q.front(); 
            q.pop();
            auto neighbors = g[u];
            for(int v = neighbors._Find_first(); 
                v < (int)neighbors.size(); 
                v = neighbors._Find_next(v))
            {
                if(color[v] == -1){
                    color[v] = 1 - color[u];
                    if(color[v]==0) c0++; else c1++;
                    q.push(v);
                } else {
                    if(color[v] == color[u]){
                        conflict = true;
                        break;
                    }
                }
            }
        }
        if(conflict){
            cout << 0 << endl;
            return 0;
        }
        comps.push_back({c0,c1});
    }

    vector<ll> dp(n+1, 0LL);
    dp[0] = 1;

    for(auto &comp : comps){
        int c0 = comp.first, c1 = comp.second;
        vector<ll> newdp(n+1, 0LL);
        for(int x=0; x<=n; x++){
            ll ways = dp[x];
            if(!ways) continue;
            if(c0 != c1){
                if(x + c0 <= n)
                    newdp[x + c0] = (newdp[x + c0] + ways) % MOD;
                if(x + c1 <= n)
                    newdp[x + c1] = (newdp[x + c1] + ways) % MOD;
            } else {
                if(x + c0 <= n)
                    newdp[x + c0] = (newdp[x + c0] + ways*2) % MOD;
            }
        }
        dp = move(newdp);
    }

    ll ans = 0;
    for(int x = max(0, n-m); x <= min(n,m); x++)
        ans = (ans + dp[x]) % MOD;
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 6584
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using i64 = long long;
#define endl '\n'
#define var auto
const ll mod = 1e9+7;
int solve(int n, int m, vector<int>&a) {
  // int n, m;
  // cin >> n >> m;
  // vector<int> a(n);
  // for (int i = 0; i < n; i++) {
  //   cin >> a[i];
  // }
  int mx = 0;
  int sx = 0;
  int lmx = 0;
  using pii = pair<int, int>;
  vector<pii> v;
  vector<int> st;
  for (int i = 0; i < n; i++) {
    if (sx > a[i]) {
      return 0;
    }
    if (mx < a[i]) {
      mx = a[i];
      st.push_back(a[i]);
    } else {
      int c = 0;
      while (!st.empty() && st.back() > a[i]) {
        c++;
        st.pop_back();
      }
      if (st.empty() && lmx > a[i]) {
        if (v.empty()) {
          v.emplace_back(c, 1);
        } else {
          v.back().first += c;
          v.back().second += 1;
        }
        lmx = mx;
        sx = a[i];
      } else {
        while (!st.empty()) {
          v.emplace_back(1, 0);
          st.pop_back();
        }
        v.emplace_back(c, 1);
        sx = a[i];
        lmx = mx;
      }
    }
  }
  while (!st.empty()) {
    v.emplace_back(1, 0);
    st.pop_back();
  }

  // int i = 0;
  // while (i < n) {
  //   int c = 0;
  //   while (i + c < n && mx < a[i + c]) {
  //     mx = a[i + c];
  //     c++;
  //   }
  //   if (i + c < n && sx > a[i + c]) {
  //     return 0;
  //   }
  //   if (i + c == n) {
  //     for (int j = 0; j < c; j++) {
  //       v.emplace_back(1, 0);
  //     }
  //     break;
  //   }
  //   int d = c;
  //   while (d >= 1 && a[i + d - 1] > a[i + c]) {
  //     d--;
  //   }
  //   for (int j = 0; j < d; j++) {
  //     v.emplace_back(1, 0);
  //   }
  //   mx = a[i + c - 1];
  //   int e = c + 1;
  //   while (i + e < n && a[i + e - 1] < a[i + e] && a[i + e] < mx) {
  //     e++;
  //   }
  //   sx = a[i + e - 1];
  //   // cout << c << " " << d << " " << mx << " " << sx << endl;
  //   v.emplace_back(c - d, e - c);
  //   i = i + e;
  // }
  int nn = v.size();
  vector<vector<ll>> dp(nn+1, vector(m + 1, 0LL));
  dp[0][0] = 1;
  // cout << "--" << endl;
  int acc = 0;
  int i = 0;
  // for (int i = 0; i < n; i++) {
  //   cout << a[i] << " \n"[i+1==n];
  // }
  for (auto [x, y] : v) {
    //    cout << x << " " << y << endl;
    for (int j = 0; j <= m && j <= acc; j++) {
      int k = acc - j;
      if (j + x <= m && k + y <= m) {
        dp[i + 1][j + x] += dp[i][j];
        if (dp[i + 1][j + x] >= mod) dp[i + 1][j + x] -= mod;
      }
      if (j + y <= m && k + x <= m) {
        dp[i + 1][j + y] += dp[i][j];
        if (dp[i + 1][j + y] >= mod) dp[i + 1][j + y] -= mod;
      }
    }
    acc += x + y;
    // cout << x << " " << y << endl;
    i++;
  }
  ll ans = 0;
  for (int i = 0; i <= m; i++) {
    ans += dp[nn][i];
    if (ans >= mod) ans -= mod;
  }
  return ans;
}
void gen(int &n, int &m, vector<int>&a) {
  n = 6;
  m = rand() % 4 + 3;
  a = vector<int>(n);
  set<int> st;

  for(int i = 0; i < n; i++) {
    st.insert(i + 1);
  }
  for(int i = 0; i < n; i++) {
    int now = -1;
    while(st.count(now) == 0) {
      now = rand() % n + 1;
    }
    st.erase(now);
    a[i] = now;
  }
  return;
}
int naive(int &n, int &m, vector<int>&a) {
  int res = 0;
  vector<int> tmpa, tmpb;
  for (int b = 0; b < (1 << n); b++) {
    bool flag = true;
    vector<int> s1{-1};
    vector<int> s2{-1};
    for (int i = 0; i < n; i++) {
      if (b >> i & 1) {
        if (s1[s1.size()-1] >= a[i]) {
          flag = false;
        }
        s1.emplace_back(a[i]);
      }
      else {
        if (s2[s2.size()-1] >= a[i]) {
          flag = false;
        }
        s2.emplace_back(a[i]);
      }
    }
    if (s1.size() <= m+1 && s2.size() <= m+1 && flag) res++;
  }
  return res;
}
int main() {
  cin.tie(0)->sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << solve(n, m, a) << endl;
  // for(int i = 0; i < 1000000; i++) {
  //   // cout << i << endl;
  //   // cout << flush;
  //   srand(i);
  //   int n, m;
  //   vector<int>a;
  //   gen(n, m, a);
  //   int sol = solve(n, m, a);
  //   int nai = naive(n, m, a);
  //   cout << sol << " " << nai << endl;
  //   if (sol != nai) {
  //     cout << "!" << endl;
  //     return;
  //   }
  //   // if (sol != 0) break;
  //
  // }
}
accuracy: 1
time_ms: 50
memory_kb: 100896
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

ll mod = 1000000007;

void add(ll &a, ll b){
	a += b;
	a %= mod;
}

int main(){
	int n, m;
	cin >> n >> m;
	vector<int> s(n);
	for(int i = 0; i < n; ++i) cin >> s[i];
	vector<vector<int>> G(n);
	for(int i = 0; i < n; ++i){
		for(int j = i+1; j < n; ++j){
			if(s[i] > s[j]){
				G[i].push_back(j);
				G[j].push_back(i);
			}
		}
	}

	vector<int> seen(n, -1);
	vector<vector<ll>> sz;

	for(int i = 0; i < n; ++i){
		if(seen[i] != -1) continue;
		seen[i] = 0;
		queue<int> q;
		q.push(i);
		sz.push_back({0, 0});
		sz.back()[seen[i]]++;
		while(!q.empty()){
			int tmp = q.front();
			q.pop();
			for(auto nxt: G[tmp]){
				if(seen[nxt] == -1){
					seen[nxt] = seen[tmp]^1;
					q.push(nxt);
					sz.back()[seen[nxt]]++;
				}else{
					if(seen[nxt] == seen[tmp]){
						cout << 0 << endl;
						return 0;
					}
				}
			}
		}
	}

	n = sz.size();
	vector<vector<ll>> dp(n+1, vector<ll>(m+1));
	dp[0][0] = 1;
	ll sum = 0;
	for(int i = 0; i < n; ++i){
		for(int j = 0; j <= m; ++j){
			if(j+sz[i][0] <= m && sum-j+sz[i][1] <= m){
				add(dp[i+1][j+sz[i][0]], dp[i][j]);
			}
			if(sum-j+sz[i][0] <= m && j+sz[i][1] <= m){
				add(dp[i+1][j+sz[i][1]], dp[i][j]);
			}
		}
		sum += sz[i][0] + sz[i][1];
	}
	ll ans = 0;
	for(int i = 0; i <= m; ++i){
		add(ans, dp[n][i]);
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 101632
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#define ALL(x) x.begin(), x.end()
using ll = long long;
using ld = long double;
using namespace std;
#define dbg(x) cerr << #x << ':' << (x) << endl;


int main()
{
    int n,m;
    cin >> n >> m;

    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) --a[i];
    
    set<int> st;
    for (int i = 0; i < n; i++) st.insert(i);
    vector<int> idx(n);
    for (int i = 0; i < n; i++) idx[a[i]] = i;

    ll mod = 1e9+7;
    vector<ll> dp(n+1), ndp(n+1);
    int bi = -1, bmx = -1;
    bool ok = true;
    dp[0] = 1;
    while (!st.empty()) {
        int i = idx[*st.begin()];
        int mx = -1;
        int mn = INT_MAX;
        for (int j = bi+1; j < i; j++) {
            if (mx > a[j]) ok = false;
            st.erase(a[j]);
            mx = max(mx,a[j]);
            mn = min(mn,a[j]);
        }
        if (!ok) break;

        for (int j = 0; j <= n; j++) {
            if (j+1 <= n && bmx < mn) ndp[j+1] = (ndp[j+1] + dp[j]) % mod;
            if (0 <= bi-j+2 && bi-j+2 <= n && bmx < a[i]) ndp[bi-j+2] = (ndp[bi-j+2] + dp[j]) % mod; 
        }

        bi = i;
        if (mx != -1) bmx = mx;
        st.erase(st.begin());
        swap(dp,ndp);
        for (int j = 0; j <= n; j++) ndp[j] = 0;
    }

    ll ans = 0;
    for (int i = n-m; i <= m; i++) {
        ans += dp[i];
        ans %= mod;
    }

    if (ok) cout << ans << endl;
    else cout << 0 << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 3784
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>
#include <numeric>

const long long MOD{1000000007};

long long norm(long long v) {
    return ((v % MOD) + MOD) % MOD;
}

long long add(long long L, long long R) {
    return norm(norm(L) + norm(R));
}

long long mult(long long L, long long R) {
    return norm(norm(L) * norm(R));
}

int N, M;
int S[5010];

int lds() {
    std::vector<int> dp(N, 1);
    for (int i{1} ; i < N ; i++) {
        for (int j{} ; j < i ; j++) if (S[j] > S[i]) {
            dp[i] = std::max(dp[i], dp[j] + 1);
        }
    }
    return *std::max_element(dp.begin(), dp.end());
}

long long solve() {
    int L{lds()};
    assert(L >= 1);
    if (L >= 3) return 0LL;
    std::vector<std::vector<int>> g(N);
    for (int i{} ; i < N ; i++) for (int j{i + 1} ; j < N ; j++) if (S[i] > S[j]) {
        g[i].push_back(j);
        g[j].push_back(i);
    }
    std::vector<int> col(N, -1);
    auto dfs{[&](auto dfs, int v, int c, int& A, int& B) -> bool {
        if (c == 0) A++;
        else if (c == 1) B++;
        else assert(false);
        col[v] = c;
        for (auto x : g[v]) {
            if (col[v] == col[x]) return false;
            if (col[x] == -1 and !dfs(dfs, x, c ^ 1, A, B)) return false;
        }
        return true;
    }};
    std::vector<long long> dp(N + 1);
    dp[0] = 1;
    for (int v{} ; v < N ; v++) if (col[v] == -1) {
        int A{}, B{};
        assert(dfs(dfs, v, 0, A, B));
        std::vector<long long> next(N + 1);
        for (int i{} ; i <= N ; i++) {
            if (i + A <= N) next[i + A] = add(next[i + A], dp[i]);
            if (i + B <= N) next[i + B] = add(next[i + B], dp[i]);
        }
        dp = std::move(next);
    }
    long long ans{};
    for (int i{N - M} ; i <= M ; i++) ans = add(ans, dp[i]);
    return ans;
}

long long brute() {
    long long ans{};
    for (int bit{} ; bit < (1 << N) ; bit++) {
        std::vector<int> A, B;
        for (int i{} ; i < N ; i++) {
            if (bit & (1 << i)) A.push_back(S[i]);
            else B.push_back(S[i]);
        }
        bool ok{true};
        ok &= A.size() <= (size_t)M;
        ok &= B.size() <= (size_t)M;
        for (int i{} ; i + 1 < (int)A.size() ; i++) ok &= A[i] < A[i + 1];
        for (int i{} ; i + 1 < (int)B.size() ; i++) ok &= B[i] < B[i + 1];
        ans += ok;
    }
    return ans;
}

int main() {
    // for (N = 1 ; N <= 8 ; N++) {
    //     std::cout << "start " << N << std::endl;
    //     std::iota(S, S + N, 0);
    //     do {
    //         for (M = (N + 1) / 2 ; M <= N ; M++) {
    //             long long a{solve()}, b{brute()};
    //             if (a != b) {
    //                 std::cout << M << std::endl;
    //                 for (int i{} ; i < N ; i++) std::cout << S[i] + 1 << ' ';
    //                 std::cout << std::endl;
    //                 std::cout << "you " << a << std::endl;
    //                 std::cout << "ans " << b << std::endl;
    //                 std::exit(0);
    //             }
    //         }
    //     } while (std::next_permutation(S, S + N));
    // }
    std::cin >> N >> M;
    for (int i{} ; i < N ; i++) {
        std::cin >> S[i];
        S[i]--;
    }
    std::cout << solve() << '\n';
}
accuracy: 1
time_ms: 210
memory_kb: 6692
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(obj) (obj).begin(),(obj).end()
#define rep(i,n,m) for(int (i)=(n);(i)<(m);(i)++)
#define MOD 1000000007
int main(){
	int n,m;
	cin>>n>>m;
	vector<ll>A(n);
	rep(i,0,n){
		cin>>A[i];
		A[i]--;
	}
	vector<vector<ll>>B(n);
	rep(i,0,n){
		rep(j,i+1,n){
			if(A[i]>A[j]){
				B[i].push_back(j);
				B[j].push_back(i);
			}
		}
	}
	vector<pair<ll,ll>>C;
	vector<ll>D(n,0);
	queue<ll>Q;
	rep(r,0,n){
		if(D[r]==0){
			
			Q.push(r);
			D[r]=1;
			int e=0,f=0;
			e++;
			while(!Q.empty()){
				ll q=Q.front();
				Q.pop();
				for(auto i:B[q]){
					if(D[q]==D[i]){
						cout<<0<<endl;
						return 0;
					}
					if(D[i]==0){
						Q.push(i);
						D[i]=3-D[q];
						if(D[i]==1)e++;
						else f++;
					}
				}
			}
			C.push_back({e,f});
		}
	}
	ll g=C.size();
	// cout<<"B";
	// for(auto i:C){
	// 	cout<<"A"<<endl;
	// 	cout<<i.first<<" "<<i.second<<endl;
	// }
	vector<vector<ll>>dp(g+1,vector<ll>(n+10,0));
	dp[0][0]=1;
	rep(i,0,g){
		rep(j,0,n){
			if(j+C[i].first<n+10)dp[i+1][j+C[i].first]+=dp[i][j];
			if(j+C[i].first<n+10)dp[i+1][j+C[i].first]%=MOD;
			if(j+C[i].second<n+10)dp[i+1][j+C[i].second]+=dp[i][j];
			if(j+C[i].second<n+10)dp[i+1][j+C[i].second]%=MOD;
		}
	}
	ll ans=0;
	rep(i,0,n){

		if(max(i,n-i)<=m){
			ans+=dp[g][i];
			ans%=MOD;
		}
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 199260
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(obj) (obj).begin(),(obj).end()
#define rep(i,n,m) for(int (i)=(n);(i)<(m);(i)++)
#define MOD 1000000007
int main(){
	int n,m;
	cin>>n>>m;
	vector<ll>A(n);
	rep(i,0,n){
		cin>>A[i];
		A[i]--;
	}
	vector<vector<ll>>B(n);
	rep(i,0,n){
		rep(j,i+1,n){
			if(A[i]>A[j]){
				B[i].push_back(j);
				B[j].push_back(i);
			}
		}
	}
	vector<pair<ll,ll>>C;
	vector<ll>D(n,0);
	rep(r,0,n){
		if(D[r]==0){
			queue<ll>Q;
			Q.push(r);
			D[r]=1;
			int e=0,f=0;
			e++;
			while(!Q.empty()){
				ll q=Q.front();
				Q.pop();
				for(auto i:B[q]){
					if(D[q]==D[i]){
						cout<<0<<endl;
						return 0;
					}
					if(D[i]==0){
						Q.push(i);
						D[i]=3-D[q];
						if(D[i]==1)e++;
						else f++;
					}
				}
			}
			C.push_back({e,f});
		}
	}
	ll g=C.size();
	// cout<<"B";
	// for(auto i:C){
	// 	cout<<"A"<<endl;
	// 	cout<<i.first<<" "<<i.second<<endl;
	// }
	vector<vector<ll>>dp(g+1,vector<ll>(n+10,0));
	dp[0][0]=1;
	rep(i,0,g){
		rep(j,0,n){
			dp[i+1][j+C[i].first]+=dp[i][j];
			dp[i+1][j+C[i].first]%=MOD;
			dp[i+1][j+C[i].second]+=dp[i][j];
			dp[i+1][j+C[i].second]%=MOD;

		}
	}
	ll ans=0;
	rep(i,0,n){

		if(max(i,n-i)<=m){
			ans+=dp[g][i];
			ans%=MOD;
		}
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 0.7435897435897436
time_ms: 140
memory_kb: 199236
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    long long N, M;
    cin >> N >> M;
    vector<long long> A(N);
    vector<pair<long long, long long>> li(N);
    for(int i=0; i<N; i++) {
        cin >> A.at(i);
        A.at(i)--;
        li.at(i)={A.at(i), i};
    }
    vector<vector<vector<long long>>> dp(1, vector<vector<long long>>(N+1, vector<long long>(2)));
    sort(li.begin(), li.end());
    dp.at(0).at(0).at(0)=1;
    long long now=1, bo=N, C=N;
    vector<vector<long long>> base(N+1, vector<long long>(2));
    for(int i=0; i<N; i++) {
        long long P=li.at(N-1-i).second;
        if (P>bo) {
            continue;
        }
        dp.push_back(base);
        now++;
        for(int j=P+2; j<bo; j++) {
            if (A.at(j-1)>A.at(j)) {
                cout << 0 << endl;
                return 0;
            }
        }
        long long co=bo-(P+1);
        // cout << now << " " << P << " " << bo << endl;
        // cout << A.at(bo-1) << " " << A.at(P) << " " << C << endl;
        if (A.at(bo-1)<C || co==0) {
            for(int j=0; j<=N-1; j++) {
                dp.at(now-1).at(j+1).at(0)+=dp.at(now-2).at(j).at(0);
                dp.at(now-1).at(j+1).at(1)+=dp.at(now-2).at(j).at(1);
            }
        }
        if (A.at(P)<C) {
            for(int j=0; j<=N-co; j++) {
                dp.at(now-1).at(j+co).at(0)+=dp.at(now-2).at(j).at(1);
                dp.at(now-1).at(j+co).at(1)+=dp.at(now-2).at(j).at(0);
            }
        }
        for(int j=P; j<bo; j++) {
            C=min(C, A.at(j));
        }
        bo=P;
        // for(int j=0; j<N+1; j++) {
        //     for(int k=0; k<2; k++) {
        //         cout << dp.at(now-1).at(j).at(k) << " "; 
        //     }
        //     cout << endl;
        // }
    }
    long long ans=0;
    for(int i=N-M; i<M+1; i++) {
        ans+=dp.at(now-1).at(i).at(0);
        ans+=dp.at(now-1).at(i).at(1);
        ans%=1000000007;
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.02564102564102564
time_ms: 20
memory_kb: 32352
final_rank: 20

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

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> S(N);
    rep(i,0,N) cin >> S[i];
    vector<vector<int>> G(N);
    rep(i,0,N) {
        rep(j,i+1,N) {
            if (S[i] > S[j]) {
                G[i].push_back(j);
                G[j].push_back(i);
            }
        }
    }
    vector<int> used(N,-1);
    vector<ll> DP(N+1,0);
    DP[0] = 1;
    rep(i,0,N) {
        if (used[i] >= 0) continue;
        int C[2] = {};
        queue<int> Q;
        used[i] = 0;
        C[0]++;
        Q.push(i);
        while(!Q.empty()) {
            int P = Q.front();
            Q.pop();
            for (int NP : G[P]) {
                if (used[NP] >= 0 && used[NP] == used[P]) {
                    cout << 0 << endl;
                    return 0;
                }
                if (used[NP] == -1) {
                    used[NP] = 1 - used[P];
                    C[used[NP]]++;
                    Q.push(NP);
                }
            }
        }
        vector<ll> NewDP(N+1,0);
        rep(i,0,N) {
            if (DP[i] > 0) {
                NewDP[i+C[0]] += DP[i], NewDP[i+C[0]] %= 1000000007;
                NewDP[i+C[1]] += DP[i], NewDP[i+C[1]] %= 1000000007;
            }
        }
        swap(DP,NewDP);
    }
    ll ANS = 0;
    rep(i,0,N+1) {
        if (max(i,N-i) <= M) ANS += DP[i];
    }
    cout << ANS % 1000000007 << endl;
}
accuracy: 1
time_ms: 180
memory_kb: 6560
final_rank: 7

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
#define MOD 1000000007
// #define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


template< int mod >
struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt< MOD >;
using mvi = vector<modint>;
using mvvi = vector<mvi>;

int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n,m;cin >> n >> m;
  vi s(n);cin >> s;
  mvvi dp(n+1,mvi(n+1));
  dp[0][0] = 1;
  rep(i,n){
    int deka = -1,tisa = i,cnt = 1;
    bool is = true;
    for(int j = i-1;j >= 0;j--){
      if(deka == -1 && s[j] > s[tisa]){
        for(int k = i-1;k > j;k--){
          if(s[k] > s[tisa]){is = false;break;}
          else tisa = k,cnt++;
        }
        deka = j;
      }else if(s[j] > s[tisa]){
        if(s[j] < s[deka]){
          for(int k = deka-1;k > j;k--){
            if(s[k] > s[tisa]){is = false;break;}
            else tisa = k,cnt++;
          }
          deka = j;
        }else {
          is = false;break;
        }
      }
      if(!is)break;
    }
    if(!is)break;
    int idx = min(deka,tisa);
    if(deka == -1)idx = i;
    rep(j,idx+1){
      if(j+cnt <= n)dp[i+1][j+cnt] += dp[idx][j];
      if(idx-j+cnt <= n)dp[i+1][idx-j+cnt] += dp[idx][j];
    }
  }
  modint res = 0;
  rep(i,n+1){
    if(i > m || n-i > m)continue;
    res += dp[n][i];
  }
  cout << res << "\n";
  // rep(i,n+1)cout << dp[i] << " a\n";
  

  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 101024
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
const ll MOD = 1e9 + 7;

const ll nmax = 5010;
ll dp[nmax][nmax];

int main() {
  ll N, M; cin >> N >> M;
  vector<ll> A(N);
  for (auto &a : A) cin >> a;
  
  reverse(A.begin(), A.end());
  vector<ll> B(N);
  vector<ll> inv(N+1, -1);
  for (int i = 0; i < N; ++i) {
    B[i] = N - A[i];
    inv[B[i]] = i;
  }
  
  bool flg = true;
  vector<pair<int, int>> pr;
  ll mn = 0, mx = 0, len = 0;
  vector<int> tmp;
  for (int i = 0; i < N; ++i) {
    len++;
    mx = max(mx, B[i]);
    tmp.emplace_back(B[i]);
    // cout << i << " " << mn << " " << mx << endl;
    
    if (mx - mn + 1 == len) {
      vector<int> v1, v2;
      int t1 = -1, t2 = -1;
      for (int j = mn; j <= mx; ++j) {
        int t = B[j];
        if (t1 > t2) swap(t1, t2), swap(v1, v2);
        // t1 <= t2
        if (t < t1) flg = false;
        else if (t < t2) {
          v1.emplace_back(t);
          t1 = t;
        }
        else {
          v2.emplace_back(t);
          t2 = t;
        }
      }
      pr.push_back({v1.size(), v2.size()});
      
      
      // cout << mn << " " << mx << endl;
      // cout << v1.size() << " " << v2.size() << endl;
      
      mn = mx = i + 1;
      len = 0;
    }
    else {
      ;
    }
    
  }
  
  if (!flg) {
    cout << 0 << endl;
    return 0;
  }
  
  map<array<ll, 2>, ll> dp, ndp;
  dp[{0, 0}] = 1;
  for (auto [v1, v2] : pr) {
    for (auto [key, value] : dp) {
      value %= MOD;
      auto [s1, s2] = key;
      int t1, t2;
      
      {
        t1 = s1 + v1, t2 = s2 + v2;
        if (t1 > t2) swap(t1, t2);
        if (t1 <= M && t2 <= M) {
          ndp[{t1, t2}] += value;
        }
        
        t1 = s1 + v2, t2 = s2 + v1;
        if (t1 > t2) swap(t1, t2);
        if (t1 <= M && t2 <= M) {
          ndp[{t1, t2}] += value;
        }
      }
    }
    
    swap(dp, ndp);
    ndp.clear();
  }
  
  ll res = 0;
  for (auto [key, value] : dp) {
    res += value;
    res %= MOD;
  }
  res %= MOD;
  
  cout << res << endl;
  
  return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3840
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

const ll mod = 1e9 + 7;
int main() {
    ll N, K;
    cin >> N >> K;
    vector<ll> S(N);
    for (int i = 0; i < N; i++) {
        cin >> S[i];
        S[i]--;
    }

    vector<bool> HG(N, 0);
    for (int i = 0; i < N; i++) {
        int x = -1;
        for (int j = 0; j < i; j++)if (S[i] < S[j]) {
            if (x > S[j]) {
                cout << 0 << endl;
                return 0;
            }
            x = S[j];
        }
        for (int j = 0; j < i; j++)if (S[i] < S[j])S[j] = x;
        if (x >= 0)HG[S[i]] = 1;
    }

    vector<vector<ll>> DP(N + 1, vector<ll>(K + 1, 0));
    vector<bool> US(N, 0);
    DP[1][1] = 2;
    US[S[0]] = 1;
    for (int i = 1; i < N; i++) {
        for (int k = 0; k <= K; k++) {
            if (US[S[i]]) {
                if (k != K) {
                    DP[i + 1][k + 1] += DP[i][k];
                    DP[i + 1][k + 1] %= mod;
                }
            }
            else if (!HG[S[i]]) {
                if (k != K) {
                    DP[i + 1][k + 1] += DP[i][k];
                    DP[i + 1][k + 1] %= mod;
                }
                if (i - k + 1 <= K) {
                    DP[i + 1][i - k + 1] += DP[i][k];
                    DP[i + 1][i - k + 1] %= mod;
                }
            }
            else {
                if (i - k + 1 <= K) {
                    DP[i + 1][k] += DP[i][k];
                    DP[i + 1][k] %= mod;
                }
            }
        }
        US[S[i]] = 1;
    }
    ll an = 0;
    for (int k = 0; k <= K; k++)an += DP[N][k];
    cout << an % mod << endl;
}
accuracy: 1
time_ms: 130
memory_kb: 198736
final_rank: 16

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

template < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }
template < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }
template < class T > vector< T >  operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }
template < class T > vector< T >  operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }

} // namespace macro

using namespace macro;
#line 1 "cp-library/src/data_structure/union_find.hpp"
class union_find {
  public:
    union_find(int n) : data(n, -1) {}
    int unite(int x, int y) {
        x = root(x), y = root(y);
        if(x != y) {
            if(size(x) < size(y)) swap(x, y);
            data[x] += data[y];
            return data[y] = x;
        }
        return -1;
    }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    bool same(int x, int y) { return root(x) == root(y); }

  private:
    vector<int> data;
};
#line 2 "cp-library/src/number/modint.hpp"
struct modinfo { uint mod, root, isprime; };
template < modinfo const &ref >
struct modint {
    static constexpr uint const &mod = ref.mod;
    static constexpr uint const &root = ref.root;
    static constexpr uint const &isprime = ref.isprime;
    uint v = 0;
    constexpr modint& s(uint v) { this->v = v < mod ? v : v - mod; return *this; }
    constexpr modint(ll v = 0) { s(v % mod + mod); }
    modint operator-() const { return modint() - *this; }
    modint& operator+=(const modint& rhs) { return s(v + rhs.v); }
    modint& operator-=(const modint& rhs) { return s(v + mod - rhs.v); }
    modint& operator*=(const modint& rhs) { v = ull(v) * rhs.v % mod; return *this; }
    modint& operator/=(const modint& rhs) { return *this *= inv(rhs); }
    modint operator+(const modint& rhs) const { return modint(*this) += rhs; }
    modint operator-(const modint& rhs) const { return modint(*this) -= rhs; }
    modint operator*(const modint& rhs) const { return modint(*this) *= rhs; }
    modint operator/(const modint& rhs) const { return modint(*this) /= rhs; }
    friend modint pow(modint x, ll n) { modint res(1); while(n > 0) { if(n & 1) res *= x; x *= x; n >>= 1; } return res; }
    friend modint inv(modint v) {
        if(isprime) {
            return pow(v, mod - 2);
        } else {
            ll a = v.v, b = modint::mod, x = 1, y = 0, t;
            while(b > 0) { t = a / b; swap(a -= t * b, b); swap(x -= t * y, y); }
            return modint(x);
        }
    }
    friend modint operator+(int x, const modint& y) { return modint(x) + y; }
    friend modint operator-(int x, const modint& y) { return modint(x) - y; }
    friend modint operator*(int x, const modint& y) { return modint(x) * y; }
    friend modint operator/(int x, const modint& y) { return modint(x) / y; }
    friend istream& operator>>(istream& is, modint& m) { ll x; is >> x; m = modint(x); return is; }
    friend ostream& operator<<(ostream& os, const modint& m) { return os << m.v; }
    bool operator==(const modint& r) const { return v == r.v; }
    bool operator!=(const modint& r) const { return v != r.v; }
    static uint get_mod() { return mod; }
    static int is_prime() { return isprime; }
};
constexpr modinfo base998244353 { 998244353, 3, 1 };
constexpr modinfo base1000000007 { 1000000007, 0, 1 };
using mint998244353 = modint< base998244353 >;
using mint1000000007 = modint< base1000000007 >;
#line 5 "A.cpp"

int main() {
    int n = in(), m = in();
    vector<int> a = in(n);

    vector<vector<int>> g(n);
    for(int i : rep(n)) for(int j : rep(i + 1, n)) {
        if(a[i] > a[j]) {
            g[i].push_back(j);
            g[j].push_back(i);
        }
    }

    bool is_bi = [&] {
        union_find uf(n + n);
        for(int u : rep(n)) for(int v : g[u]) uf.unite(u, n + v);
        for(int i : rep(n)) if(uf.same(i, i + n)) return false;
        return true;
    }();
    if(not is_bi) return print(0);

    vector<pair<int,int>> b;
    vector<int> vis(n, 0);
    for(int i : rep(n)) if(not vis[i]) {
        int c0 = 0, c1 = 0;
        auto dfs = [&](auto self, int v, int c) -> void {
            vis[v] = true;
            (c == 0 ? c0 : c1) += 1;
            for(int to : g[v]) {
                if(not vis[to]) self(self, to, c ^ 1);
            }
        }; dfs(dfs, i, 0);
        b.push_back({c0, c1});
    }
    
    using mint = mint1000000007;
    vector<mint> dp(m + 1, 0);
    dp[0] = 1;
    int sum = 0;
    for(auto [x, y] : b) {
        vector<mint> nt(m + 1, 0);
        for(int p = 0; p <= m; p++) {
            if(p + x <= m and (sum - p) + y <= m) nt[p + x] += dp[p];
            if(p + y <= m and (sum - p) + x <= m) nt[p + y] += dp[p];
        }
        dp = move(nt);
        sum += x + y;
    }
    mint ans = 0;
    for(int p = 0; p <= m; p++) ans += dp[p];
    print(ans);
}
accuracy: 1
time_ms: 30
memory_kb: 6884
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define per(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define each(e, x) for (auto &e : x)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define eb emplace_back
#define MM << ' ' <<
#define TT template <typename T>
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

TT bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }
TT bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }

TT using minheap = priority_queue<T, vector<T>, greater<T>>;
TT using maxheap = priority_queue<T>;

TT void print(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (n == 0) cout << '\n';
}

TT void printn(vector<T> v, T x = 0) {
    int n = sz(v);
    rep(i, n) cout << v[i] + x << '\n';
}

TT void rearrange(vector<T> &v) {
    sort(all(v));
    v.erase(unique(all(v)), end(v));
}

TT int lb(vector<T> &v, T x) { return lower_bound(all(v), x) - begin(v); }
TT int ub(vector<T> &v, T x) { return upper_bound(all(v), x) - begin(v); }

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

template <int mod>
struct modint {
    int x;

    modint() : x(0) {}

    modint(ll y) {
        x = y % mod;
        if (x < 0) x += mod;
    }

    static int get_mod() { return mod; }

    modint &operator+=(modint p) {
        x += p.x;
        if (x >= mod) x -= mod;
        return *this;
    }

    modint &operator-=(modint p) {
        x -= p.x;
        if (x < 0) x += mod;
        return *this;
    }

    modint &operator*=(modint p) {
        x = (1LL * x * p.x) % mod;
        return *this;
    }

    modint &operator/=(modint p) {
        *this *= p.inverse();
        return *this;
    }

    modint operator-() { return modint(-x); }

    modint operator+(modint p) { return modint(*this) += p; }

    modint operator-(modint p) { return modint(*this) -= p; }

    modint operator*(modint p) { return modint(*this) *= p; }

    modint operator/(modint p) { return modint(*this) /= p; }

    bool operator==(modint p) { return x == p.x; }

    bool operator!=(modint p) { return x != p.x; }

    modint pow(ll k) {
        modint ret = 1, x = *this;
        while (k > 0) {
            if (k & 1) ret *= x;
            x *= x;
            k >>= 1;
        }
        return ret;
    }

    modint inverse() { return pow(mod - 2); }

    friend ostream &operator<<(ostream &os, modint p) { return os << p.x; }

    friend istream &operator>>(istream &is, modint &p) {
        ll y;
        is >> y;
        p = modint(y);
        return is;
    }
};
const int MOD = 1000000007;
using mint = modint<MOD>;

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> s(n);
    rep(i, n) cin >> s[i];
    vector<mint> dp(n + 1, 0);
    dp[0] = 1;
    int idx = 0, ma = -1;
    while (idx < n) {
        int mi = 1e9, pm = -1;
        rep2(i, idx, n) if (chmin(mi, s[i])) pm = i;
        int mi2 = 1e9;
        rep2(i, idx, pm) chmin(mi2, s[i]);
        vector<mint> ndp(n + 1, 0);
        bool ok = true;
        rep2(i, idx, pm - 1) ok &= s[i] < s[i + 1];
        int l = pm - idx;
        if (ok) {
            rep(i, idx + 1) {
                if (idx - i + l <= n && mi > ma)
                    ndp[idx - i + l] += dp[i];
                if (i + l <= n && mi2 > ma)
                    ndp[i + l] += dp[i];
            }
        }
        swap(dp, ndp);
        rep2(i, idx, pm + 1) chmax(ma, s[i]);
        idx = pm + 1;
    }
    mint ans = 0;
    rep2(i, n - m, m + 1) ans += dp[i];
    cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3512
final_rank: 2

code_snippet: #include <bits/stdc++.h>
//  03:24

constexpr int mod = 1000000007;

int main() {
    int n, m;
    std::cin >> n >> m;
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) std::cin >> a[i];

    std::vector graph(n, std::vector<int>());
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (a[i] > a[j]) {
                graph[i].push_back(j);
                graph[j].push_back(i);
            }
        }
    }

    std::vector<std::pair<int, int>> bip_size;
    const int UNDEF = -1;
    const int BLACK = 0;
    const int WHITE = 1;
    std::vector<int> color(n, UNDEF);

    auto dfs = [&](auto self, int u) -> std::tuple<int, int, bool> {
        assert(color[u] != UNDEF);
        int white = 0;
        int black = 0;
        for (int v: graph[u]) {
            if (color[v] == UNDEF) {
                color[v] = color[u] ^ 1;
                auto [dw, db, ok] = self(self, v);
                if (!ok) return {-1, -1, false};
                white += dw;
                black += db;
            } else if (color[v] == color[u]) {
                return {-1, -1, false};
            }
        }
        if (color[u] == WHITE) white++;
        else black++;
        return {white, black, true};
    };

    for (int i = 0; i < n; i++) {
        if (color[i] == UNDEF) {
            color[i] = WHITE;
            auto [w, b, ok] = dfs(dfs, i);
            if (!ok) {
                std::cout << 0 << std::endl;
                return 0;
            }
            bip_size.emplace_back(w, b);
        }
    }

    std::vector dp(m + 1, std::vector<int>(m + 1));
    dp[0][0] = 1;
    int sum = 0;
    for (auto [x, y]: bip_size) {
        for (int i = 0; i <= sum; i++) {
            int j = sum - i;
            if (i > m || j > m) continue;
            if (i + x <= m && j + y <= m) {
                dp[i + x][j + y] += dp[i][j];
                if (dp[i + x][j + y] >= mod) {
                    dp[i + x][j + y] -= mod;
                }
            }
            if (i + y <= m && j + x <= m) {
                dp[i + y][j + x] += dp[i][j];
                if (dp[i + y][j + x] >= mod) {
                    dp[i + y][j + x] -= mod;
                }
            }
        }
        sum += x + y;
    }

    int ans = 0;
    for (int i = 0; i <= m; i++) {
        int j = n - i;
        if (i > m || j > m) continue;
        ans += dp[i][j];
        if (ans >= mod) {
            ans -= mod;
        }
    }
    std::cout << ans << std::endl;
}
accuracy: 1
time_ms: 70
memory_kb: 101508
final_rank: 11