<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:
        lol = 10**9 + 7
        col_options = []
        for i in range(0, 3**m):
            works = True
            for j in range(0, m-1):
                jth_trit = (i // (3**j)) % 3
                j1th_trit = (i // (3**(j+1))) % 3
                if jth_trit == j1th_trit:
                    works = False
                    break
            if works:
                col_options.append(i)
        next_cols = {}
        for col1 in col_options:
            next_cols[col1] = []
            for col2 in col_options:
                if col2 == -1:
                    continue
                works = True
                for j in range(0, m):
                    trit1 = (col1 // (3**j)) % 3
                    trit2 = (col2 // (3**j)) % 3
                    if trit1 == trit2:
                        works = False
                        break
                if works:
                    next_cols[col1].append(col2)
        next_cols[-1] = list(col_options)
        cur_amount = {num : 0 for num in col_options}
        cur_amount[-1] = 1
        col_options.append(-1)
        #print(next_cols)
        for i in range(0, n):
            print(cur_amount)
            next_amount = {num : 0 for num in col_options}
            for cur_col in next_cols:
                for next_col in next_cols[cur_col]:
                    next_amount[next_col] = (next_amount[next_col] + cur_amount[cur_col]) % lol
            cur_amount = next_amount
        #print(cur_amount)
        result = 0
        for col in cur_amount:
            result = (result + cur_amount[col]) % lol
        return result
411