<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
        
        # Function to check if two columns are valid neighbors
        def is_valid(col1, col2):
            for i in range(m):
                if col1[i] == col2[i]:
                    return False
            return True
        
        # Generate all possible columns
        def generate_columns(m):
            if m == 1:
                return [(1,), (2,), (3,)]
            columns = []
            for col in generate_columns(m - 1):
                for color in range(1, 4):
                    if col[-1] != color:
                        columns.append(col + (color,))
            return columns
        
        # Generate all possible columns for the given m
        columns = generate_columns(m)
        
        # Create a dictionary to store the index of each column
        col_index = {col: i for i, col in enumerate(columns)}
        
        # Create a 2D DP array where dp[i][j] represents the number of ways to color the first i columns ending with column j
        dp = [[0] * len(columns) for _ in range(n)]
        
        # Base case: The first column can be any valid column
        for j in range(len(columns)):
            dp[0][j] = 1
        
        # Fill the DP array
        for i in range(1, n):
            for j in range(len(columns)):
                for k in range(len(columns)):
                    if is_valid(columns[j], columns[k]):
                        dp[i][j] = (dp[i][j] + dp[i-1][k]) % MOD
        
        # Sum up the number of ways to color the last column
        result = sum(dp[n-1][j] for j in range(len(columns))) % MOD
        
        return result

# Example usage:
solution = Solution()
m = 1
n = 1
print(solution.colorTheGrid(m, n))  # Output: 3

m = 1
n = 2
print(solution.colorTheGrid(m, n))  # Output: 6

m = 5
n = 5
print(solution.colorTheGrid(m, n))  # Output: 580986
1820