<p>You have a <code>grid</code> of size <code>n x 3</code> and you want to paint each cell of the grid with exactly one of the three colors: <strong>Red</strong>, <strong>Yellow,</strong> or <strong>Green</strong> while making sure that no two adjacent cells have the same color (i.e., no two cells that share vertical or horizontal sides have the same color).</p>

<p>Given <code>n</code> the number of rows of the grid, return <em>the number of ways</em> you can paint this <code>grid</code>. As the answer may grow large, the answer <strong>must be</strong> computed modulo <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/2020/03/26/e1.png" style="width: 400px; height: 257px;" />
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 12
<strong>Explanation:</strong> There are 12 possible way to paint the grid as shown.
</pre>

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

<pre>
<strong>Input:</strong> n = 5000
<strong>Output:</strong> 30228214
</pre>

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

<ul>
	<li><code>n == grid.length</code></li>
	<li><code>1 &lt;= n &lt;= 5000</code></li>
</ul>

class Solution:
    def numOfWays(self, n: int) -> int:
        mod = 10**9 +7
        # d = {
        #     "RYR": ["YRG", "YGY", "YRY", "GRG", "GRY"],
        #     "RYG": ["YRY", "YGR", "YGY", "GRY"],
        #     "RGR": ["YRG", "YRY", "GRG", "GRY", "GYG"],
        #     "RGY": ["YRG", "GYG", "GYR", "GRG"]
        # }
        # if ABA: [3 ABA, 2 ABC], if ABC: [2 ABA 2 ABC]
        # For cnt, [6 ABA 6 ABC]
        res = 0
        options = self.dp(n, mod)
        for i in options:
            for j in options[i]:
                for k in options[i][j]:
                    res += options[i][j][k]
        return res%mod
    
    def dp(self, m, mod):
        if m == 1:
            return self.option(1)
        option = self.dp(m-1, mod)
        new_option = self.option(0)
        for i in option:
            for j in option[i]:
                for k in option[i][j]:
                    samples = self.samples(i,j,k,option[i][j][k])
                    for a in samples:
                        for b in samples[a]:
                            for c in samples[a][b]:
                                new_option[a][b][c] += samples[a][b][c]
                                new_option[a][b][c] %= mod
        return new_option

    def samples(self, i, j, k, freq):
        cnt = self.option(0)
        for a in ("R", "Y", "G"):
            if a != i:
                for b in ("R", "Y", "G"):
                    if b != j and b != a:
                        for c in ("R", "Y", "G"):
                            if c != k and c != b:
                                cnt[a][b][c] += freq
        return cnt

    def option(self, val):
        return {
                "R": {
                    "Y": {
                        "G": val,
                        "R": val
                    },
                    "G": {
                        "R": val,
                        "Y": val
                    }
                },
                "G": {
                    "Y": {
                        "G": val,
                        "R": val
                    },
                    "R": {
                        "G": val,
                        "Y": val
                    }
                },
                "Y": {
                    "R": {
                        "G": val,
                        "Y": val
                    },
                    "G": {
                        "R": val,
                        "Y": val
                    }
                }
            }
         
18000