<p>You have two types of tiles: a <code>2 x 1</code> domino shape and a tromino shape. You may rotate these shapes.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/15/lc-domino.jpg" style="width: 362px; height: 195px;" />
<p>Given an integer n, return <em>the number of ways to tile an</em> <code>2 x n</code> <em>board</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/15/lc-domino1.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The five different ways are show above.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 1000</code></li>
</ul>

class Solution {
public:

    const int mod=1e9+7;

    int func(int i, int up, int down, vector<vector<vector<int>>> &dp, int n){
        if(i>=n){
            if(up==0 && down==0) return 1;
            if(i==n && ((up==2 && down==0) || (up==0 && down==2))) return 1;
            return 0;
        }
        if(dp[i][up][down]!=-1) return dp[i][up][down];
        int p1=0,p2=0,p3=0,p4=0,p5=0;
        if(up==0 && down==0){
            p1=func(i+1,0,0,dp,n);
            p2=func(i+2,2,0,dp,n);
            // p3=func(i+2,0,2,dp,n);
            p4=func(i+2,1,0,dp,n);
            p5=func(i+2,0,1,dp,n);
        }
        else if(up==0 && down==1){
            p1=func(i+1,1,0,dp,n);
            p2=func(i+1,0,0,dp,n);
        }
        else if(up==1 && down==0){
            p1=func(i+1,0,1,dp,n);
            p2=func(i+1,0,0,dp,n);
        }
        else if(up==2 && down==0){
            p1=func(i,0,0,dp,n);
        }
        else if(up==0 && down==2){
            p1=func(i,0,0,dp,n);
        }
        int tot=p1;
        tot=(tot+p2)%mod;
        // tot=(tot+p3)%mod;
        tot=(tot+p4)%mod;
        tot=(tot+p5)%mod;
        return dp[i][up][down]=tot;
    }

    int numTilings(int n) {
        vector<vector<vector<int>>> dp(n, vector<vector<int>>(3, vector<int>(3, -1)));
        return func(0, 0, 0, dp, n);
    }
};
11700