<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, mod = 1_000_000_007) -> int:

        if m == 1:
            return 3*pow(2, n-1, mod) %mod

        if m == 2:
            return 2*pow(3, n, mod) %mod

        if m == 3:
            x0, x1 = 0, 3

            for _ in range(n):
                x0, x1 = ((3*x0 + 2*x1) %mod, 
                          (2*x0 + 2*x1) %mod)

            return (x0+x1) %mod   

        if m == 4:
            x0, x1, x2 = 0, 2, 2

            for _ in range(n):
                x0, x1, x2 = ((3*x0 + 2*x1 +   x2) %mod,
                              (4*x0 + 4*x1 + 2*x2) %mod,
                              (  x0 +   x1 + 2*x2) %mod)

            return (x0+ x1 + x2) %mod

        if m == 5:
            (x0, x1, x2, x3, x4, x5, x6, x7) = (6, 6, 6, 6, 6, 6, 6, 6)

            for _ in range(n-1):

                (x0, x1, x2, x3, x4, x5, x6, x7) = (

                    (3*x0 + 2*x1 + 2*x2 +   x3 +          x5 + 2*x6 + 2*x7) %mod,
                    (2*x0 + 2*x1 + 2*x2 +   x3 +   x4 +   x5 +   x6 +   x7) %mod,
                    (2*x0 + 2*x1 + 2*x2 +   x3 +          x5 + 2*x6 + 2*x7) %mod,
                    (1*x0 + 1*x1 + 1*x2 + 2*x3 +   x4 +   x5 +   x6 +   x7) %mod,
                    (     + 1*x1 +      +   x3 + 2*x4 +   x5 +          x7) %mod,
                    (1*x0 + 1*x1 + 1*x2 +   x3 +   x4 + 2*x5 +   x6 +   x7) %mod,
                    (2*x0 + 1*x1 + 2*x2 +   x3 +          x5 + 2*x6 +   x7) %mod,
                    (2*x0 + 1*x1 + 2*x2 +   x3 +   x4 +   x5 +   x6 + 2*x7) %mod )

            return (x0 + x1 + x2 + x3 + x4 + x5 + x6 + x7) %mod
79