#include <bits/stdc++.h>
const int mod = 1000000007;
const int maxn = (1 << 16) + 10;
int ans, n, U, p[20], f[600][maxn], g[maxn], tot, root;
char s[1000];
struct tree {
  int ls, rs;
  char op;
} t[600];
inline void build(int &k, int l, int r) {
  k = ++tot;
  if (l == r) {
    t[k].op = s[l];
    return;
  }
  int p = 0;
  for (int i = l, tp = 0; i < r; i++) {
    if (s[i] == '(') ++tp;
    if (s[i] == ')') --tp;
    if (!tp) {
      p = i + 1;
      break;
    }
  }
  build(t[k].ls, l + 1, p - 2);
  build(t[k].rs, p + 2, r - 1);
  t[k].op = s[p];
}
inline void FMT(int *a, int tp, int f) {
  for (int i = 1; i < U; i <<= 1)
    for (int j = 0; j < U; j += i << 1)
      for (int k = 0; k < i; k++) {
        if (tp == 1)
          a[i + j + k] = (0LL + a[i + j + k] + f * a[j + k] + mod) % mod;
        if (tp == 2) a[j + k] = (0LL + a[j + k] + f * a[i + j + k] + mod) % mod;
      }
}
inline void dfs(int u) {
  if (!t[u].ls and !t[u].rs) {
    for (int j = 0; j < 4; j++)
      if (t[u].op == '?' or t[u].op == 'A' + j) {
        int S = 0;
        for (int i = 0; i < 16; i++)
          if ((i >> j) & 1) S |= 1 << i;
        f[u][S]++;
      }
    for (int j = 0; j < 4; j++)
      if (t[u].op == '?' or t[u].op == 'a' + j) {
        int S = 0;
        for (int i = 0; i < 16; i++)
          if (!((i >> j) & 1)) S |= 1 << i;
        f[u][S]++;
      }
    return;
  }
  dfs(t[u].ls);
  dfs(t[u].rs);
  if (t[u].op == '?' or t[u].op == '|') {
    FMT(f[t[u].ls], 1, 1);
    FMT(f[t[u].rs], 1, 1);
    for (int S = 0; S < U; S++)
      g[S] = 1LL * f[t[u].ls][S] * f[t[u].rs][S] % mod;
    FMT(f[t[u].ls], 1, -1);
    FMT(f[t[u].rs], 1, -1);
    FMT(g, 1, -1);
    for (int S = 0; S < U; S++) f[u][S] = (f[u][S] + g[S]) % mod;
  }
  if (t[u].op == '?' or t[u].op == '&') {
    FMT(f[t[u].ls], 2, 1);
    FMT(f[t[u].rs], 2, 1);
    for (int S = 0; S < U; S++)
      g[S] = 1LL * f[t[u].ls][S] * f[t[u].rs][S] % mod;
    FMT(f[t[u].ls], 2, -1);
    FMT(f[t[u].rs], 2, -1);
    FMT(g, 2, -1);
    for (int S = 0; S < U; S++) f[u][S] = (f[u][S] + g[S]) % mod;
  }
}
signed main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  U = 1 << 16;
  int m;
  scanf("%d", &m);
  for (int i = 0; i < 16; i++) p[i] = -1;
  for (int a, b, c, d, e; m--;)
    scanf("%d%d%d%d%d", &a, &b, &c, &d, &e),
        p[(d << 3) | (c << 2) | (b << 1) | a] = e;
  build(root, 1, n);
  dfs(root);
  for (int S = 0; S < U; S++) {
    bool flag = true;
    for (int i = 0; i < 16; i++)
      if (~p[i]) flag &= (S >> i) ^ p[i] ^ 1;
    if (flag and f[root][S]) ans = (ans + f[root][S]) % mod;
  }
  return !printf("%d\n", ans);
}
