#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chkmax(T &x, T y) {
  x = x > y ? x : y;
}
template <typename T>
void chkmin(T &x, T y) {
  x = x > y ? y : x;
}
template <typename T>
void add(T &x, T y, T mod) {
  x = x + y > mod ? x + y - mod : x + y;
}
template <typename T>
void sub(T &x, T y, T mod) {
  x = x - y < 0 ? x - y + mod : x - y;
}
template <typename T>
void multi(T &x, T y, T mod) {
  x = 1ll * x * y % mod;
}
const int INF = (1ll << 30);
template <typename T>
void read(T &x) {
  x = 0;
  bool f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = 0;
  } while (ch > '9' || ch < '0');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  x = f ? x : -x;
}
template <typename T>
void write(T x) {
  if (x < 0) x = ~x + 1, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const int S = (1 << 16) + 7;
const int N = 500 + 7;
const int mod = 1e9 + 7;
char a[N], s[N];
int n, m, rt, tot, tmp[S], ch[N][2], h[N][5], f[N][S];
inline void work(int &rt, int l, int r) {
  if (l > r) return;
  if (l == r) return (void)(a[rt = ++tot] = s[l]);
  int sum = 0, pos = 0;
  for (int i = l; i <= r; i++) {
    if (s[i] == '(') sum++;
    if (s[i] == ')') sum--;
    if (sum == 0) {
      pos = i;
      break;
    }
  }
  if (pos == r) return work(rt, l + 1, r - 1);
  a[rt = ++tot] = s[pos + 1];
  work(ch[rt][0], l + 1, pos - 1);
  work(ch[rt][1], pos + 2, r);
}
inline void FWT_OR(int *a, int n, int opt) {
  for (int i = 1; i < n; i <<= 1) {
    for (int j = 0; j < n; j += (i << 1)) {
      for (int k = 0; k < i; k++) {
        a[i + j + k] =
            (a[i + j + k] + 1ll * a[j + k] * (opt + mod) + mod) % mod;
      }
    }
  }
}
inline void FWT_AND(int *a, int n, int opt) {
  for (int i = 1; i < n; i <<= 1) {
    for (int j = 0; j < n; j += (i << 1)) {
      for (int k = 0; k < i; k++) {
        a[j + k] = (a[j + k] + 1ll * a[i + j + k] * (opt + mod) + mod) % mod;
      }
    }
  }
}
inline void Work_AND(int *c, int *a, int *b, int n) {
  FWT_AND(a, n, 1);
  FWT_AND(b, n, 1);
  for (int i = 0; i < n; i++) c[i] = 1ll * a[i] * b[i] % mod;
  FWT_AND(c, n, -1);
  FWT_AND(a, n, -1);
  FWT_AND(b, n, -1);
}
inline void Work_OR(int *c, int *a, int *b, int n) {
  FWT_OR(a, n, 1);
  FWT_OR(b, n, 1);
  for (int i = 0; i < n; i++) c[i] = 1ll * a[i] * b[i] % mod;
  FWT_OR(c, n, -1);
  FWT_OR(a, n, -1);
  FWT_OR(b, n, -1);
}
inline void dfs(int u) {
  if (!ch[u][0] && !ch[u][1]) {
    if (a[u] == '?') {
      for (int i = 0; i < 4; i++) {
        int k = 0;
        for (int j = 0; j < m; j++) k |= h[j][i] << j;
        add(f[u][k], 1, mod);
        add(f[u][((1 << m) - 1) ^ k], 1, mod);
      }
    } else {
      int pos, k = 0;
      if (a[u] >= 'a' && a[u] <= 'd')
        pos = a[u] - 'a';
      else
        pos = a[u] - 'A';
      for (int j = 0; j < m; j++) k |= h[j][pos] << j;
      if (a[u] >= 'a' && a[u] <= 'd')
        add(f[u][((1 << m) - 1) ^ k], 1, mod);
      else
        add(f[u][k], 1, mod);
    }
    return;
  }
  dfs(ch[u][0]);
  dfs(ch[u][1]);
  if (a[u] == '&') Work_AND(f[u], f[ch[u][0]], f[ch[u][1]], (1 << m));
  if (a[u] == '|') Work_OR(f[u], f[ch[u][0]], f[ch[u][1]], (1 << m));
  if (a[u] == '?') {
    Work_AND(tmp, f[ch[u][0]], f[ch[u][1]], (1 << m));
    Work_OR(f[u], f[ch[u][0]], f[ch[u][1]], (1 << m));
    for (int i = 0; i < (1 << m); i++) add(f[u][i], tmp[i], mod);
  }
}
inline void solve(int p) {
  if (ch[p][0]) solve(ch[p][0]);
  cout << "rt = " << p << " " << a[p] << endl;
  if (ch[p][1]) solve(ch[p][1]);
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  work(rt, 1, n);
  read(m);
  int mask = 0;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < 5; j++) read(h[i][j]);
  dfs(rt);
  for (int i = 0; i < m; i++) mask |= (h[i][4] << i);
  printf("%d\n", f[1][mask]);
  return 0;
}
