CSES - Shared codeLink to this code:
https://cses.fi/paste/9a5e593fc2bf7b252122b3/
#include <bits/stdc++.h>
using namespace std;
const int MN = 1005, MM = 12, MOD = 1e9 + 7;
int n, m, dp[MN][MM][1 << MM];
void add(int& a, int b) {
a = (a + b) % MOD;
}
int32_t main() {
cin >> m >> n;
int full = (1 << (m + 1)) - 1; //the maximum possible mask with all m+1 bits set
dp[0][m][0] = 1; //base case
for (int i = 1; i <= n; i++) {
for (int msk = 0; msk <= full; msk++) add(dp[i][0][msk << 1], dp[i - 1][m][msk]); //transfer data from row i-1 to i
for (int j = 1; j <= m; j++) {
for (int msk = 0; msk <= full; msk++) {
int rit = msk & (1 << (j - 1)), dwn = msk & (1 << j); //right and down plug from (i, j-1) respectively
if (!rit && !dwn) { //case 1
add(dp[i][j][msk ^ (1 << j)], dp[i][j - 1][msk]); //place right domino
add(dp[i][j][msk ^ (1 << (j - 1))], dp[i][j - 1][msk]); //place down domino
} else if (rit && !dwn) add(dp[i][j][msk ^ (1 << (j - 1))], dp[i][j - 1][msk]); //case 2
else if (!rit && dwn) add(dp[i][j][msk ^ (1 << j)], dp[i][j - 1][msk]); //case 3
}
}
}
printf("%d\n", dp[n][m][0]); //final answer
return 0;
}