#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &x) {
  x = 0;
  int f = 1, ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 - '0' + ch;
    ch = getchar();
  }
  x *= f;
}
const int mod = 1e9 + 7;
const int maxn = 16;
const int maxlen = 500 + 50;
const int maxs = 1 << maxn;
const int sigma = 8;
const char C[sigma] = {'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd'};
int n;
int a, b, c, d, e;
char s[maxlen];
int len;
int root;
int bit[maxn + 1];
int match[maxlen];
int ch[maxlen][2];
int dp[maxlen][maxs];
bool mark[maxlen];
inline int add(int x) { return x >= mod ? x - mod : x; }
inline int sub(int x) { return x < 0 ? x + mod : x; }
inline int idx(char x) {
  switch (x) {
    case 'A':
      return a;
    case 'a':
      return ~a & (bit[n] - 1);
    case 'B':
      return b;
    case 'b':
      return ~b & (bit[n] - 1);
    case 'C':
      return c;
    case 'c':
      return ~c & (bit[n] - 1);
    case 'D':
      return d;
    case 'd':
      return ~d & (bit[n] - 1);
    default:
      assert(0);
  }
}
void build(int &u, int l, int r) {
  if (l == r) {
    u = l;
    mark[u] = 1;
    return;
  }
  u = match[l] + 1;
  build(ch[u][0], l + 1, u - 2);
  build(ch[u][1], u + 2, r - 1);
}
void Max(int *A, int *B, int *C) {
  static int a[maxs], b[maxs];
  memcpy(a, A, sizeof(a));
  memcpy(b, B, sizeof(b));
  for (int i = 0; i < n; ++i)
    for (int s = 0; s < bit[n]; ++s)
      if (s & bit[i]) {
        a[s] = add(a[s] + a[s ^ bit[i]]);
        b[s] = add(b[s] + b[s ^ bit[i]]);
      }
  for (int s = 0; s < bit[n]; ++s) a[s] = (long long)a[s] * b[s] % mod;
  for (int i = n - 1; ~i; --i)
    for (int s = 0; s < bit[n]; ++s)
      if (s & bit[i]) {
        a[s] = sub(a[s] - a[s ^ bit[i]]);
      }
  for (int i = 0; i < bit[n]; ++i) C[i] = add(C[i] + a[i]);
}
void Min(int *A, int *B, int *C) {
  static int a[maxs], b[maxs];
  memcpy(a, A, sizeof(a));
  memcpy(b, B, sizeof(b));
  for (int i = 0; i < n; ++i)
    for (int s = 0; s < bit[n]; ++s)
      if (~s & bit[i]) {
        a[s] = add(a[s] + a[s ^ bit[i]]);
        b[s] = add(b[s] + b[s ^ bit[i]]);
      }
  for (int s = 0; s < bit[n]; ++s) a[s] = (long long)a[s] * b[s] % mod;
  for (int i = n - 1; ~i; --i)
    for (int s = 0; s < bit[n]; ++s)
      if (~s & bit[i]) {
        a[s] = sub(a[s] - a[s ^ bit[i]]);
      }
  for (int i = 0; i < bit[n]; ++i) C[i] = add(C[i] + a[i]);
}
void DP(int u) {
  if (mark[u]) {
    if (s[u] == '?') {
      for (int i = 0; i < sigma; ++i) {
        ++dp[u][idx(C[i])];
      }
    } else
      dp[u][idx(s[u])] = 1;
    return;
  }
  DP(ch[u][0]);
  DP(ch[u][1]);
  if (s[u] != '&') Max(dp[ch[u][0]], dp[ch[u][1]], dp[u]);
  if (s[u] != '|') Min(dp[ch[u][0]], dp[ch[u][1]], dp[u]);
}
void init() {
  static int sta[maxn];
  int top = 0;
  scanf("%s", s + 1);
  len = strlen(s + 1);
  for (int i = 1; i <= len; ++i) {
    if (s[i] == '(') {
      sta[++top] = i;
    } else if (s[i] == ')') {
      match[sta[top--]] = i;
    }
  }
  build(root, 1, len);
}
void init_bit() {
  bit[0] = 1;
  for (int i = 1; i <= n; ++i) bit[i] = bit[i - 1] << 1;
}
int main() {
  init();
  read(n);
  init_bit();
  for (int i = 0; i < n; ++i) {
    int A, B, C, D, E;
    read(A), read(B), read(C), read(D), read(E);
    a ^= A * bit[i];
    b ^= B * bit[i];
    c ^= C * bit[i];
    d ^= D * bit[i];
    e ^= E * bit[i];
  }
  if (n == 0) {
    int an = 1;
    for (int i = 1; i <= len; ++i) {
      if (s[i] == '?') {
        an = an * (mark[i] ? 8ll : 2) % mod;
      }
    }
    printf("%d\n", an);
    return 0;
  }
  DP(root);
  printf("%d\n", dp[root][e]);
  return 0;
}
