Python count solution
\n# find all possible patterns for a single row: [rgb, rbg, ...]\ndef findComb(s):\n\tif len(s) < m:\n\t\tfor c in "rgb":\n\t\t\tif c != s[-1]:\n\t\t\t\tfindCom
```\n# find all possible patterns for a single row: [rgb, rbg, ...]\ndef findComb(s):\n\tif len(s) < m:\n\t\tfor c in "rgb":\n\t\t\tif c != s[-1]:\n\t\t\t\tfindComb(s + c)\n\telse:\n\t\tks.append(s)\nks = []\nfor c in "rgb":\n\tfindComb(c)\n\n# find all possible pairs: {rgb: [gbr, brg, ...], brg: [gbr, ...]}\nh = {}\nfor k in ks:\n\th[k] = []\n\tfor v in ks:\n\t\tif all(k[i] != v[i] for i in range(m)):\n\t\t\th[k].append(v)\n\n# count row by row\nresult = {k : 1 for k in ks}\nfor _ in range(n - 1):\n\ttemp = {k : 0 for k in ks}\n\tfor k in ks:\n\t\tfor v in h[k]:\n\t\t\ttemp[k] += result[v]\n\tresult = temp\n\n# return sum\nreturn sum(result.values()) % (10 ** 9 + 7)\n```
[]