[Solution] Rubik’s Cube Coloring (hard version) solution codeforces

Rubik’s Cube Coloring (hard version) solution codeforces


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π‘˜βˆ’12kβˆ’1 nodesΒ β€” a binary tree where all vertices 𝑖i from 11 to 2π‘˜βˆ’1βˆ’12kβˆ’1βˆ’1 have exactly two children: vertices 2𝑖2i and 2𝑖+12i+1. Vertices from 2π‘˜βˆ’12kβˆ’1 to 2π‘˜βˆ’12kβˆ’1 don’t have any children. You want to color its vertices with the 66 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 109+7109+7.

Rubik’s Cube Coloring (hard version) solution codeforces

Input

The first line contains the integers π‘˜k (1β‰€π‘˜β‰€601≀k≀60)Β β€” the number of levels in the perfect binary tree you need to color.

The second line contains the integer 𝑛n (1≀𝑛≀min(2π‘˜βˆ’1,2000)1≀n≀min(2kβˆ’1,2000))Β β€” the number of nodes, colors of which are already chosen.

The next 𝑛n lines contains integer 𝑣v (1≀𝑣≀2π‘˜βˆ’11≀v≀2kβˆ’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 at most once.

Rubik’s Cube Coloring (hard version) solution codeforces

Output

Print one integerΒ β€” the number of the different colorings modulo 109+7109+7.

Rubik’s Cube Coloring (hard version) solution codeforces

Examples

input

Copy
3
2
5 orange
2 white
output

Copy
1024

Rubik’s Cube Coloring (hard version) solution codeforces

input

Copy
2
2
1 white
2 white
output

Copy
0

Rubik’s Cube Coloring (hard version) solution codeforces

input

Copy
10
3
1 blue
4 red
5 orange
output

Copy
328925088

Rubik’s Cube Coloring (hard version) solution codeforces

Note

In the picture below, you can see one of the correct colorings of the first test example.



Rubik's Cube Coloring (hard version) solution codeforcesYet another MEX problem solution codechefYet another MEX problem solution codechefYet another MEX problem solution codechef

Leave a Comment