<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
        
        # Generate all valid colorings for a single column
        def generate_valid_columns(m):
            colors = [0, 1, 2]  # 0: Red, 1: Green, 2: Blue
            valid_columns = []
            
            def backtrack(column):
                if len(column) == m:
                    valid_columns.append(tuple(column))
                    return
                for color in colors:
                    if not column or column[-1] != color:
                        column.append(color)
                        backtrack(column)
                        column.pop()
            
            backtrack([])
            return valid_columns
        
        valid_columns = generate_valid_columns(m)
        column_count = len(valid_columns)
        
        # Precompute transitions
        transitions = [[] for _ in range(column_count)]
        for i in range(column_count):
            for j in range(column_count):
                if all(valid_columns[i][k] != valid_columns[j][k] for k in range(m)):
                    transitions[i].append(j)
        
        # DP table
        dp = [[0] * column_count for _ in range(n)]
        
        # Initialize the first column
        for i in range(column_count):
            dp[0][i] = 1
        
        # Fill the DP table
        for col in range(1, n):
            for i in range(column_count):
                for j in transitions[i]:
                    dp[col][j] = (dp[col][j] + dp[col - 1][i]) % MOD
        
        # Sum up all ways to color the last column
        return sum(dp[n - 1]) % MOD

# Example usage:
# solution = Solution()
# print(solution.colorTheGrid(1, 1))  # Output: 3
# print(solution.colorTheGrid(1, 2))  # Output: 6
# print(solution.colorTheGrid(5, 5))  # Output: 580986
18400