human
PYTHON solution for "It is the hard version of the problem. The difference is that in this version, there are nodes with already chosen colors.

Theofanis is starving, and he wants to eat his favorite food, sheftalia. However, he should first finish his homework. Can you help him with this problem?

You have a perfect binary tree of 2^k - 1 nodes — a binary tree where all vertices i from 1 to 2^{k - 1} - 1 have exactly two children: vertices 2i and 2i + 1. Vertices from 2^{k - 1} to 2^k - 1 don't have any children. You want to color its vertices with the 6 Rubik's cube colors (White, Green, Red, Blue, Orange and Yellow).

Let's call a coloring good when all edges connect nodes with colors that are neighboring sides in the Rubik's cube.

|   
---|---  
A picture of Rubik's cube and its 2D map.

More formally: 

  * a white node can not be neighboring with white and yellow nodes.

  * a yellow node can not be neighboring with white and yellow nodes.

  * a green node can not be neighboring with green and blue nodes.

  * a blue node can not be neighboring with green and blue nodes.

  * a red node can not be neighboring with red and orange nodes.

  * an orange node can not be neighboring with red and orange nodes.



However, there are n special nodes in the tree, colors of which are already chosen.

You want to calculate the number of the good colorings of the binary tree. Two colorings are considered different if at least one node is colored with a different color.

The answer may be too large, so output the answer modulo 10^9+7.

Input

The first line contains the integers k (1 ≤ k ≤ 60) — the number of levels in the perfect binary tree you need to color.

The second line contains the integer n (1 ≤ n ≤ min(2^k - 1, 2000)) — the number of nodes, colors of which are already chosen.

The next n lines contains integer v (1 ≤ v ≤ 2^k - 1) and string s — the index of the node and the color of the node (s is one of the white, yellow, green, blue, red and orange).

It is guaranteed that each node v appears in the input...":


 ... Can you provide a Python code to calculate the number of the good colorings of the binary tree?
assistant
#Here is Python code to calculate the number of the good colorings of the binary tree.
 def main():
    k = int(raw_input())
    n = int(raw_input())
    c = "white yellow green blue red orange".split()
    d = {x: j for j, x in enumerate(c)}
    a = [{} for _ in xrange(k + 1)]
    for i in xrange(n):
        x, s = raw_input().split()
        x = int(x)
        l = x.bit_length()
        w = [0] * 6
        w[d[s]] = 1
        a[l][x] = w
    mod = 1000000007
    f = [pow(4, pow(2, i, mod - 1) + mod - 3, mod) for i in xrange(k + 1)]
    for i in xrange(k, 0, -1):
        if i < k:
            for x in a[i]:
                t = a[i][x]
                for b in xrange(2):
                    y = x * 2 + b
                    if y in a[i+1]:
                        w = a[i+1][y]
                    else:
                        w = [f[k-i]] * 6
                    s = sum(w)
                    for j in xrange(6):
                        t[j] = t[j] * (s - w[j] - w[j^1]) % mod
        for x in a[i]:
            if x / 2 not in a[i-1]:
                a[i-1][x/2] = [1] * 6
    print sum(a[1][1]) % mod
main()