[JavaScript] DP with Bit masking
Time O(n * (2^m) * (2^m))\nSpace O(n * 2^(2 * m))\n\nvar colorTheGrid = function (m, n) {\n    let dp = Array.from({ length: n }, () => Array(1 << (2 * m)));\n\
**Time O(n * (2^m) * (2^m))\nSpace O(n * 2^(2 * m))**\n```\nvar colorTheGrid = function (m, n) {\n    let dp = Array.from({ length: n }, () => Array(1 << (2 * m)));\n\n    const mod = 1e9 + 7;\n\n    const dfs = (rowIdx, colIdx, prevRowColor) => {\n        if (rowIdx === n) return 1;\n\n        if (colIdx === 0 && dp[rowIdx][prevRowColor])\n            return dp[rowIdx][prevRowColor];\n\n        let possibilities = 0;\n\n        const topCell = (prevRowColor >> (2 * colIdx)) & 3,\n            leftCell = (prevRowColor >> (2 * (colIdx - 1))) & 3;\n\n        for (let i = 1; i <= 3; i++) {\n            if (topCell !== i && leftCell !== i) {\n                possibilities =\n                    (possibilities +\n                        dfs(\n                            rowIdx + (colIdx === m - 1),\n                            (colIdx + 1) % m,\n                            (prevRowColor ^ (topCell << (2 * colIdx))) | (i << (2 * colIdx))\n                    )) %\n                    mod;\n            }\n        }\n\n        if (colIdx === 0) dp[rowIdx][prevRowColor] = possibilities;\n\n        return possibilities;\n    };\n\n    return dfs(0, 0, 0);\n};\n\n```
[]