The chess knight has a unique movement,&nbsp;it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:

A chess knight can move as indicated in the chess diagram below:

We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell&nbsp;(i.e. blue cell).

Given an integer n, return how many distinct phone numbers of length n we can dial.

You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.

As the answer may be very large, return the answer modulo 109 + 7.

&nbsp;
Example 1:

Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.


Example 2:

Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]


Example 3:

Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.


&nbsp;
Constraints:


	1 &lt;= n &lt;= 5000


class Solution:
    def knightDialer(self, n: int) -> int:
        # Sum elements of matrix modulo mod subroutine
        def sum_mat(matrix, mod):
            return sum(sum(row) % mod for row in matrix) % mod
        
        # Matrix multiplication subroutine
        def mult_mat(a, b):
            return [[sum(a[i][k]*b[k][j] for k in range(10)) for j in range(10)] for i in range(10)]
        
        # Matrix exponentiation subroutine
        def pow_mat(matrix, k, dp):
            if k not in dp:
                if k == 0:
                    dp[k] = [[(1 if i == j else 0) for j in range(10)] for i in range(10)]
                else:
                    dp[k] = pow_mat(matrix, k//2, dp)
                    dp[k] = mult_mat(dp[k], dp[k])
                    if k % 2:
                        dp[k] = mult_mat(dp[k], matrix)
            return dp[k]
       
        # Create matrix
        edges = [(1, 6), (1, 8), (2, 9), (2, 7), (3, 4), (3, 8), (4, 0), (4, 9), (6, 1), (6, 0), (6, 7)]
        matrix =  [[0 for j in range(10)] for i in range(10)]
        for i, j in edges:
            matrix[i][j] = 1
            matrix[j][i] = 1
        
		# Solve
        mod = 10**9 + 7
        return sum_mat(pow_mat(matrix, n-1, {}), mod)
class Solution {
    public int knightDialer(int n) {
        var dp = new long[10];
        var tmp = new long[10];
        Arrays.fill(dp, 1);
        for (int i = 1; i < n; i++) {
            tmp[1] = dp[6]+dp[8];
            tmp[2] = dp[7]+dp[9];
            tmp[3] = dp[4]+dp[8];
            tmp[4] = dp[0]+dp[3]+dp[9];
            tmp[5] = 0;
            tmp[6] = dp[0]+dp[1]+dp[7];
            tmp[7] = dp[2]+dp[6];
            tmp[8] = dp[1]+dp[3];
            tmp[9] = dp[2]+dp[4];
            tmp[0] = dp[4]+dp[6];
            for (int j = 0; j < 10; j++) tmp[j] = tmp[j] % 1000000007;
            var arr = dp;
            dp = tmp;
            tmp = arr;
        }
        long res = 0;
        for (int i = 0; i < 10; i++) {
            res = (res+dp[i]) % 1000000007;
        }
        return (int)res;
    }
}
class Solution {
    int MOD=1e9+7;
public:
    int knightDialer(int n) {
        if(n==1) return 10;
        int a=2, b=2, c=3, d=2; //a{2,8} b{1,3,7,9} c{4,6} d{0}
        for(int i=3; i<=n; i++){
            int w, x, y, z;
            w = 2ll*b%MOD;
            x = (1ll*a + 1ll*c)%MOD;
            y = (2ll*b + 1ll*d)%MOD;
            z = 2ll*c%MOD;
            a = w; b = x; c = y; d = z;
        }
        int ans = (2ll*a + 4ll*b + 2ll*c + d)%MOD;
        return ans;
    }
};
var knightDialer = function(n) {

    let dp = Array(10).fill(1)
    let MOD = 10**9 + 7

    for(let i = 2; i <= n ; i++) {
        oldDp = [...dp]
        dp[0] = (oldDp[4] + oldDp[6]) % MOD
        dp[1] = (oldDp[8] + oldDp[6]) % MOD
        dp[2] = (oldDp[9] + oldDp[7]) % MOD
        dp[3] = (oldDp[8] + oldDp[4]) % MOD
        dp[4] = (oldDp[3] + oldDp[9] + oldDp[0]) % MOD
        dp[5] = 0
        dp[6] = (oldDp[0] + oldDp[7] + oldDp[1]) % MOD
        dp[7] = (oldDp[6] + oldDp[2]) % MOD
        dp[8] = (oldDp[3] + oldDp[1]) % MOD
        dp[9] = (oldDp[4] + oldDp[2]) % MOD
    }

    return dp.reduce((ans, ele) => ans += ele, 0) % MOD
};
Knight Dialer