<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>

import numpy as np

class Solution:
    def colorTheGrid(self, m: int, n: int) -> int:
        M = 10**9+7
        if m == 1: return (pow(2, n-1, M)*3) % M
        if m == 2: return (pow(3, n-1, M)*6) % M
        v1 = self.choose_vec(m)
        A = self.choose_mat(m)
        A = self.modexp(A, n-1, M)
        return sum(np.matmul(A, v1)) % M

    def choose_vec(self, m):
        if m == 3: return np.array([6, 6], dtype=int)
        if m == 4: return np.array([6, 12, 6], dtype=int)
        if m == 5: return np.array([6, 6, 6, 6, 6, 6, 6, 6], dtype=int)

    def choose_mat(self, m):
        if m == 3: return np.array([[2, 2], [2, 3]], dtype=int)
        if m == 4: return np.array([[3, 2, 1], [4, 4, 2], [1, 1, 2]], dtype=int)
        if m == 5: return np.array([[3, 2, 2, 1, 0, 1, 2, 2],
                                    [2, 2, 2, 1, 1, 1, 1, 1],
                                    [2, 2, 2, 1, 0, 1, 2, 2],
                                    [1, 1, 1, 2, 1, 1, 1, 1],
                                    [0, 1, 0, 1, 2, 1, 0, 1],
                                    [1, 1, 1, 1, 1, 2, 1, 1],
                                    [2, 1, 2, 1, 0, 1, 2, 1],
                                    [2, 1, 2, 1, 1, 1, 1, 2]
                                    ], dtype=int)

    def modexp(self, A, k, M):
        n = A.shape[0]
        res = np.identity(n, dtype=int)
        while k:
            if k % 2 == 1:
                res = np.matmul(res, A) % M
            A = np.matmul(A, A) % M
            k //= 2
        return res     
162