Link to this code:
https://cses.fi/paste/52bd3ac7d4f28363286a4a//*
author: @ankingcodes
created: 2021-08-08 08:43:09.737451
*/
#include<bits/stdc++.h>
#include<algorithm>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define INF 1e18
#define f first
#define s second
#define mp make_pair
#define pb push_back
const int MOD = (int) 1e9 + 7;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> PBDS;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
tree_order_statistics_node_update> pairPBDS;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m; cin >> n >> m;
int dp[n+1][m+1] = {};
// special case: first index
int prev;
int x; cin >> x;
if (x == 0) for (int i=0;i<=m;i++) dp[0][i] = 1;
else dp[0][x] = 1;
// for i > 0, no. of ways to fill up to i, is sum of prev val
for (int i=1;i<n;i++) {
int x; cin >> x;
prev = x;
if (x == 0) {
for (int j=1;j<=m;j++) {
for (int k: {j-1, j, j+1})
if (k >= 1 && k <= m)
(dp[i][j] += dp[i-1][k])%=MOD;
}
} else {
for (int k: {x-1, x, x+1})
if (k >= 1 && k <= m)
(dp[i][x] += dp[i-1][k])%=MOD;
}
}
// why this step ?
int ans = 0;
for (int i=1;i<=m;i++)
(ans += dp[n-1][i])%=MOD;
cout << ans << endl;
return 0;
}