#include <bits/stdc++.h>
using namespace std;
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
template <int m>
struct mod {
  long long x;
  mod() : x(0) {}
  mod(long long xx) : x(xx) {
    if (abs(x) >= m) x %= m;
    if (x < 0) x += m;
  }
  mod operator+(const mod &a) const {
    mod n;
    n.x = x + a.x;
    if (n.x >= m) n.x -= m;
    return n;
  }
  mod operator-(const mod &a) const {
    mod n;
    n.x = x - a.x;
    if (n.x < 0) n.x += m;
    return n;
  }
  mod operator*(const mod &a) const { return x * a.x; }
  mod operator+=(const mod &a) {
    x += a.x;
    if (x >= m) x -= m;
    return *this;
  }
  mod operator-=(const mod &a) {
    x -= a.x;
    if (x < 0) x += m;
    return *this;
  }
  mod operator*=(const mod &a) {
    x = (x * a.x) % m;
    return *this;
  }
  mod pow(long long b) const {
    mod ans = 1;
    mod a = *this;
    while (b > 0) {
      if (b & 1) ans *= a;
      a *= a;
      b /= 2;
    }
    return ans;
  }
  mod inv() const { return pow(m - 2); }
  mod operator/(const mod &a) const { return (*this) * a.inv(); }
  mod operator/=(const mod &a) { return (*this) *= a.inv(); }
  bool operator==(const mod &o) const { return x == o.x; }
  bool operator!=(const mod &o) const { return x != o.x; }
  long long operator()() const { return x; }
};
template <int m>
istream &operator>>(istream &is, mod<m> &n) {
  long long x;
  is >> x;
  n = x;
  return is;
}
template <int m>
ostream &operator<<(ostream &os, const mod<m> &n) {
  return os << n();
}
const int M = 1e9 + 7;
using base = mod<M>;
map<string, int> ma;
vector<base> dp(60);
int k;
struct node {
  int col = -1;
  node *left = NULL, *right = NULL;
  node() {}
  node(int col) : col(col) {}
};
int lg(long long x) { return 63 - __builtin_clzll(x); }
void add(node *r, long long i, long long idx, int c, int layer = k) {
  if (i == idx) {
    r->col = c;
    return;
  }
  if ((~idx >> (lg(idx) + layer - k - 1)) & 1) {
    if (r->left == NULL) r->left = new node;
    add(r->left, 2 * i, idx, c, layer - 1);
  } else {
    if (r->right == NULL) r->right = new node;
    add(r->right, 2 * i + 1, idx, c, layer - 1);
  }
}
array<base, 3> solve(node *r, long long i, int layer = k) {
  array<base, 3> a = {0, 0, 0};
  if (r == NULL) {
    a[0] = a[1] = a[2] = dp[layer - 1] * 2;
    return a;
  }
  if (layer == 1) {
    if (r->col == -1)
      a[0] = a[1] = a[2] = 2;
    else
      a[r->col] = 1;
    return a;
  }
  auto b = solve(r->left, 2 * i, layer - 1);
  auto c = solve(r->right, 2 * i + 1, layer - 1);
  for (int i = (0); i < (3); i++) {
    int ii = (i + 1) % 3;
    int iii = (i + 2) % 3;
    if (r->col != -1 && r->col != i) {
      a[i] = 0;
      continue;
    }
    a[i] = (b[ii] + b[iii]) * (c[ii] + c[iii]);
    if (r->col == -1) a[i] *= 2;
  }
  return a;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ma["white"] = ma["yellow"] = 0;
  ma["red"] = ma["orange"] = 1;
  ma["green"] = ma["blue"] = 2;
  cin >> k;
  dp[0] = 1;
  for (int i = (1); i < (k); i++) {
    dp[i] = dp[i - 1] * dp[i - 1] * 4 * 4;
  }
  int n;
  cin >> n;
  node *root = new node();
  for (int i = (0); i < (n); i++) {
    long long idx;
    string s;
    cin >> idx >> s;
    int c = ma[s];
    add(root, 1, idx, c);
  }
  auto a = solve(root, 1);
  cout << a[0] + a[1] + a[2] << '\n';
}

### Prompt

Please formulate a Cpp solution to the following problem:

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.

<image>| <image>  
---|---  
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 at most once.

Output

Print one integer — the number of the different colorings modulo 10^9+7.

Examples

Input


3
2
5 orange
2 white


Output


1024


Input


2
2
1 white
2 white


Output


0


Input


10
3
1 blue
4 red
5 orange


Output


328925088

Note

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

<image>

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
template <int m>
struct mod {
  long long x;
  mod() : x(0) {}
  mod(long long xx) : x(xx) {
    if (abs(x) >= m) x %= m;
    if (x < 0) x += m;
  }
  mod operator+(const mod &a) const {
    mod n;
    n.x = x + a.x;
    if (n.x >= m) n.x -= m;
    return n;
  }
  mod operator-(const mod &a) const {
    mod n;
    n.x = x - a.x;
    if (n.x < 0) n.x += m;
    return n;
  }
  mod operator*(const mod &a) const { return x * a.x; }
  mod operator+=(const mod &a) {
    x += a.x;
    if (x >= m) x -= m;
    return *this;
  }
  mod operator-=(const mod &a) {
    x -= a.x;
    if (x < 0) x += m;
    return *this;
  }
  mod operator*=(const mod &a) {
    x = (x * a.x) % m;
    return *this;
  }
  mod pow(long long b) const {
    mod ans = 1;
    mod a = *this;
    while (b > 0) {
      if (b & 1) ans *= a;
      a *= a;
      b /= 2;
    }
    return ans;
  }
  mod inv() const { return pow(m - 2); }
  mod operator/(const mod &a) const { return (*this) * a.inv(); }
  mod operator/=(const mod &a) { return (*this) *= a.inv(); }
  bool operator==(const mod &o) const { return x == o.x; }
  bool operator!=(const mod &o) const { return x != o.x; }
  long long operator()() const { return x; }
};
template <int m>
istream &operator>>(istream &is, mod<m> &n) {
  long long x;
  is >> x;
  n = x;
  return is;
}
template <int m>
ostream &operator<<(ostream &os, const mod<m> &n) {
  return os << n();
}
const int M = 1e9 + 7;
using base = mod<M>;
map<string, int> ma;
vector<base> dp(60);
int k;
struct node {
  int col = -1;
  node *left = NULL, *right = NULL;
  node() {}
  node(int col) : col(col) {}
};
int lg(long long x) { return 63 - __builtin_clzll(x); }
void add(node *r, long long i, long long idx, int c, int layer = k) {
  if (i == idx) {
    r->col = c;
    return;
  }
  if ((~idx >> (lg(idx) + layer - k - 1)) & 1) {
    if (r->left == NULL) r->left = new node;
    add(r->left, 2 * i, idx, c, layer - 1);
  } else {
    if (r->right == NULL) r->right = new node;
    add(r->right, 2 * i + 1, idx, c, layer - 1);
  }
}
array<base, 3> solve(node *r, long long i, int layer = k) {
  array<base, 3> a = {0, 0, 0};
  if (r == NULL) {
    a[0] = a[1] = a[2] = dp[layer - 1] * 2;
    return a;
  }
  if (layer == 1) {
    if (r->col == -1)
      a[0] = a[1] = a[2] = 2;
    else
      a[r->col] = 1;
    return a;
  }
  auto b = solve(r->left, 2 * i, layer - 1);
  auto c = solve(r->right, 2 * i + 1, layer - 1);
  for (int i = (0); i < (3); i++) {
    int ii = (i + 1) % 3;
    int iii = (i + 2) % 3;
    if (r->col != -1 && r->col != i) {
      a[i] = 0;
      continue;
    }
    a[i] = (b[ii] + b[iii]) * (c[ii] + c[iii]);
    if (r->col == -1) a[i] *= 2;
  }
  return a;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ma["white"] = ma["yellow"] = 0;
  ma["red"] = ma["orange"] = 1;
  ma["green"] = ma["blue"] = 2;
  cin >> k;
  dp[0] = 1;
  for (int i = (1); i < (k); i++) {
    dp[i] = dp[i - 1] * dp[i - 1] * 4 * 4;
  }
  int n;
  cin >> n;
  node *root = new node();
  for (int i = (0); i < (n); i++) {
    long long idx;
    string s;
    cin >> idx >> s;
    int c = ma[s];
    add(root, 1, idx, c);
  }
  auto a = solve(root, 1);
  cout << a[0] + a[1] + a[2] << '\n';
}

```