CSES - Shared codeLink to this code:
https://cses.fi/paste/6af2b0bd367bfb83aac5ce/
#ifdef ONLINE_JUDGE
#define debug(...);
#else
#include "dbg/template.cpp"
#endif
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define oo 1000000010
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int const mod = (int)1e9+7;
int dp[100001][101], arr[100001];
int ln,m;
int depee(int idx, int last){
if (idx==ln){
return 1;
}
if (abs(arr[idx]-last)>1 && arr[idx]){
return 0;
}
int &ret = dp[idx][last];
if (ret!=-1){
return ret;
}
ret = 0;
if (arr[idx]){
return ret = depee(idx+1,arr[idx])%mod;
}
if (idx==0){
for (int i = 1 ; i <= m ; i++){
ret=(ret+depee(idx+1,i))%mod;
}
}else{
for (int i : {last-1,last,last+1}){
if (1<=i && i<=m){
ret = (ret+depee(idx+1,i))%mod;
}
}
}
return ret;
}
void burn(){
// يارب يشتغل
cin >> ln >> m;
for (int i = 0 ; i < ln ; i++){
cin >> arr[i];
}
for (int i = 0 ; i < 100001 ; i++){ // you wrote 10001 instead of 100001
for (int j = 0 ; j < 101 ; j++){
dp[i][j] = -1;
}
}
cout << depee(0,arr[0]) << endl;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int t=1;
// cin >> t;
while (t--){
burn();
}
return (0-0);
}