<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 matmul(self, mat1, mat2, m):
        new_mat = [[0 for i in range(3 ** m)] for j in range(3 ** m)]

        for i in range(3 ** m):
            if not self.valid(i, m): continue
            for j in range(3 ** m):
                if (not self.valid(j, m)): continue
                for k in range(3 ** m):
                    new_mat[i][j] += mat1[i][k] * mat2[k][j]
        return new_mat

    def binexp(self, mat, a, ident, m):
        if (a == 0):
            return ident
        res = self.binexp(mat, a // 2, ident, m)
        if (a % 2):
            return self.matmul(self.matmul(res, res, m), mat, m)
        else:
            return self.matmul(res, res, m)

    def valid(self, col1 : int, m : int):
        curr = col1 % 3
        prev = curr
        col1 = col1 // 3
        for i in range(m-1):
            curr = col1 % 3
            if (curr == prev): return False
            prev = curr
            col1 = col1 // 3
        
        return True

    def valid_t(self, col1 : int, col2 : int, m : int):
        "assume col1 and col2 are valid"
        for i in range(m):
            c1 = col1 % 3
            c2 = col2 % 3
            if (c1 == c2): return False
            col1 = col1 // 3
            col2 = col2 // 3
        return True


    def colorTheGrid(self, m: int, n: int) -> int:
        # R = 0, B = 1, G = 2
        MOD = int(1e9 + 7)
        total = 0
        transition = [[0 for i in range(3 ** m)] for j in range(3 ** m)]
        ident = [[0 for i in range(3 ** m)] for j in range(3 ** m)]

        if n == 1:
            for i in range(3 ** m):
                total += self.valid(i, m) % MOD
            return total

        # form identity
        for i in range(3 ** m):
            ident[i][i] = 1

        # form adjacency matrix
        for i in range(3 ** m):
            for j in range(3 ** m):
                if (not self.valid(i, m)) or (not self.valid(j, m)): 
                    # print(int(transition[i][j]), end=' ')
                    continue
                if (self.valid_t(i, j, m)):
                    transition[i][j] = 1
            #     print(int(transition[i][j]), end=' ')
            # print()
        
        transition_total = self.binexp(transition, n-1, ident, m)

        for i in range(3 ** m):
            for j in range(3 ** m):
                total = (total + (transition_total[i][j] % MOD)) % MOD

        return total


# G has 3^m nodes. each node represents a potential coloring of a m-vector. an edge from node i to jode j will only exist if 

# number of 1's in A -> # ways to color mx2
# number of 1's in AA -> # way to color mx3

# (AA)_ij = sum_k (A_ik A_kj) [ikj] m by 3
# (AA)_ij represents the number of valis 2 paths from coloring i to some valid coloring that i goes to, to coloring j that the previous node can come to
#A^(n-1)


22100