No bitmask
\n Add your space complexity here, e.g. O(n) \n\n# Code\n\nb=[]\ndef gen(s,n):\n    global b\n    if len(s)==n:\n        fl=False\n        for i in range(1,n):\
\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nb=[]\ndef gen(s,n):\n    global b\n    if len(s)==n:\n        fl=False\n        for i in range(1,n):\n            fl|=s[i]==s[i-1]\n        if not fl:\n            b.append(s)\n        return\n    gen(s+"0",n)\n    gen(s+"1",n)\n    gen(s+"2",n)\nclass Solution:\n    def colorTheGrid(self, m: int, n: int) -> int:\n        global b\n        dp=defaultdict(lambda :-1)\n        mod=10**9+7\n        b=[]\n        gen("",m)\n        # print(b)\n        def rec(i,prev):\n            if i==n:\n                return 1\n            if dp[i,prev]!=-1:\n                return dp[i,prev]\n            ans=0\n            for cl in b:\n                if prev==-1 :\n                    ans+=rec(i+1,cl)\n                    ans%=mod\n                    continue\n                fl=False\n                for id in range(len(cl)):\n                    fl|=prev[id]==cl[id]\n                if fl==False:\n                    ans+=rec(i+1,cl)\n                    ans%=mod\n            dp[i,prev]=ans\n            return ans\n        x=rec(0,-1)\n        print(x)\n        return x%mod\n```
['Python3']