Python. Faster than 83%...
\nclass Solution:\n    def colorTheGrid(self, m: int, n: int) -> int:\n        def f(i,p=-1,s=\'\'):\n            if i==m:\n                res.append(s)\n     
```\nclass Solution:\n    def colorTheGrid(self, m: int, n: int) -> int:\n        def f(i,p=-1,s=\'\'):\n            if i==m:\n                res.append(s)\n                return\n            for j in range(3):\n                if j!=p:\n                    f(i+1,j,s+str(j))\n        @lru_cache(None)\n        def func(i,p):\n            if i==n:\n                return 1\n            ans=0\n            for j in pos[p]:\n                ans+=func(i+1,j)\n            return ans%(10**9+7)\n        res=[]\n        f(0)\n        di={}\n        pos=defaultdict(list)\n        for i in range(len(res)):\n            for j in range(i+1,len(res)):\n                if all(res[i][k]!=res[j][k] for k in range(m)):\n                    pos[i].append(j)\n                    pos[j].append(i)\n        pos[-1]=[i for i in range(len(res))]\n        return func(0,-1)%(10**9+7)\n```
[]