<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:
    def numTilings(self, n: int) -> int:
        # if 1 <= n <= 2:
        #     return n

        # f = [[0,0] for i in range(n)]

        # # a single vertical domino
        # f[0][0] = 1     # ways: domino tile
        # f[1][0] = 0     # ways: tromino tile
        
        # # 2 vertical or 2 horizontal dominos
        # f[1][0] = 2     
        # f[1][1] = 0

        # mod = pow(10, 9) + 7

        # for i in range(2, n):
        #     f[i][0] = (f[i - 1][0] + f[i - 1][1]) + (f[i - 2][0] + f[i - 2][1])
        #     f[i][0] = f[i][0] % mod

        #     for j in range(2, i):
        #         f[i][1] += 2 * (f[j - 2][1] + f[j - 2][0])
        #         f[i][1] = f[i][1] % mod

        #     f[i][1] += 2
        #     f[i][1] = f[i][1] % mod


        # return (f[-1][0] + f[-1][1]) % mod


        if 1 <= n <= 2:
            return n

        f = [0 for i in range(n)]

        f[0] = 1
        f[1] = 2

        mod = pow(10, 9) + 7

        prefix_sum = 0

        for i in range(2, n):
            f[i] = f[i - 1] + f[i - 2] + (2 * prefix_sum) + 2
            f[i] = f[i] % mod

            prefix_sum += f[i - 2]
            prefix_sum = prefix_sum % mod


        return f[-1]
16300