<p>You are given two integers <code>m</code> and <code>n</code>. Consider an <code>m x n</code> grid where each cell is initially white. You can paint each cell <strong>red</strong>, <strong>green</strong>, or <strong>blue</strong>. All cells <strong>must</strong> be painted.</p>

<p>Return<em> the number of ways to color the grid with <strong>no two adjacent cells having the same color</strong></em>. Since the answer can be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/22/colorthegrid.png" style="width: 200px; height: 50px;" />
<pre>
<strong>Input:</strong> m = 1, n = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The three possible colorings are shown in the image above.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/22/copy-of-colorthegrid.png" style="width: 321px; height: 121px;" />
<pre>
<strong>Input:</strong> m = 1, n = 2
<strong>Output:</strong> 6
<strong>Explanation:</strong> The six possible colorings are shown in the image above.
</pre>

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

<pre>
<strong>Input:</strong> m = 5, n = 5
<strong>Output:</strong> 580986
</pre>

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

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

class Solution:
    def colorTheGrid(self, m: int, n: int) -> int:
        MOD = 10 ** 9 + 7
        answer = 1
        if m == 1:
            answer = 3
            for i in range(0, n - 1):
                answer *= 2
        if m == 2:
            answer = 6
            for i in range(0, n - 1):
                answer *= 3
        if m == 3:
            a3 = [0, 6] #121
            b3 = [0, 6] #123
            for i in range(1, n):
                a3.append(3 * a3[i] + 2 * b3[i])
                b3.append(2 * a3[i] + 2 * b3[i])
            answer = a3[n] + b3[n]
        if m == 4:
            a4 = [0, 6] #1212
            b4 = [0, 12] #1232 or 2321
            c4 = [0, 6] #1231
            for i in range(1, n):
                a4.append(3 * a4[i] + 2 * b4[i] + 1 * c4[i])
                b4.append(4 * a4[i] + 4 * b4[i] + 2 * c4[i])
                c4.append(1 * a4[i] + 1 * b4[i] + 2 * c4[i])
            answer = a4[n] + b4[n] + c4[n]
        if m == 5:
            a5 = [0, 6] #12121
            b5 = [0, 12] #12123 or 32121
            c5 = [0, 6] #12131
            d5 = [0, 12] #12132 or 23121
            e5 = [0, 6] #12321
            f5 = [0, 6] #12312 
            for i in range(1, n):
                a5.append(3 * a5[i] + 2 * b5[i] + 2 * c5[i] + 1 * d5[i] + 2 * e5[i] + 0 * f5[i])
                b5.append(4 * a5[i] + 3 * b5[i] + 4 * c5[i] + 2 * d5[i] + 2 * e5[i] + 2 * f5[i])
                c5.append(2 * a5[i] + 2 * b5[i] + 2 * c5[i] + 1 * d5[i] + 2 * e5[i] + 0 * f5[i])
                d5.append(2 * a5[i] + 2 * b5[i] + 2 * c5[i] + 3 * d5[i] + 2 * e5[i] + 2 * f5[i])
                e5.append(2 * a5[i] + 1 * b5[i] + 2 * c5[i] + 1 * d5[i] + 2 * e5[i] + 0 * f5[i])
                f5.append(0 * a5[i] + 1 * b5[i] + 0 * c5[i] + 1 * d5[i] + 0 * e5[i] + 2 * f5[i])
            answer = a5[n] + b5[n] + c5[n] + d5[n] + e5[n] + f5[n]
        return answer % MOD
        12132
        31313

          
18200