#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long Inf = 1e10;
const int N = 100005;
const int P = 1000000007;
int IN() {
  int x = 0;
  int f = 0, ch = 0;
  for ((ch = getchar()); ch < 48 || ch > 57; (ch = getchar())) f = (ch == '-');
  for (; ch >= 48 && ch <= 57; (ch = getchar()))
    x = (x << 1) + (x << 3) + ch - 48;
  return f ? (-x) : x;
}
int Pow(int x, int y, int p) {
  int A = 1;
  for (; y; y >>= 1, x = (long long)x * x % p)
    if (y & 1) A = (long long)A * x % p;
  return A;
}
int n, A[105][5], res[105], U;
char s[100005];
int l, top, stk[100005], V[100005];
int f[505][65545], cnt;
int a[65545], b[65545];
void an(int *a, int f, int l, int r) {
  if (l + 1 == r) return;
  int md = (l + r) >> 1;
  an(a, f, l, md);
  an(a, f, md, r);
  for (int i = (int)0; i < (int)md - l; i++)
    a[l + i] = (0ll + a[l + i] + a[md + i] * f + P) % P;
}
void o(int *a, int f, int l, int r) {
  if (l + 1 == r) return;
  int md = (l + r) >> 1;
  o(a, f, l, md);
  o(a, f, md, r);
  for (int i = (int)0; i < (int)md - l; i++)
    a[md + i] = (0ll + a[md + i] + a[l + i] * f + P) % P;
}
void And(int x, int l, int r) {
  for (int i = (int)0; i < (int)U; i++) a[i] = f[l][i];
  for (int i = (int)0; i < (int)U; i++) b[i] = f[r][i];
  an(a, 1, 0, U);
  an(b, 1, 0, U);
  for (int i = (int)0; i < (int)U; i++) a[i] = 1ll * a[i] * b[i] % P;
  an(a, -1, 0, U);
  for (int i = (int)0; i < (int)U; i++) f[x][i] = (f[x][i] + a[i]) % P;
}
void Or(int x, int l, int r) {
  for (int i = (int)0; i < (int)U; i++) a[i] = f[l][i];
  for (int i = (int)0; i < (int)U; i++) b[i] = f[r][i];
  o(a, 1, 0, U);
  o(b, 1, 0, U);
  for (int i = (int)0; i < (int)U; i++) a[i] = 1ll * a[i] * b[i] % P;
  o(a, -1, 0, U);
  for (int i = (int)0; i < (int)U; i++) f[x][i] = (f[x][i] + a[i]) % P;
}
int work(int l, int r) {
  if (l == r) {
    ++cnt;
    if (isalpha(s[l])) {
      int fg = (s[l] >= 'a' && s[l] <= 'z');
      int t;
      if (s[l] >= 'a' && s[l] <= 'z') {
        t = s[l] - 'a' + 1;
      } else {
        t = s[l] - 'A' + 1;
      }
      int stat = 0;
      for (int i = (int)1; i <= (int)n; i++)
        stat |= (A[i][t] ^ fg) * (1 << (i - 1));
      f[cnt][stat]++;
    } else {
      for (int i = (int)1; i <= (int)4; i++) {
        for (int j = (int)0; j <= (int)1; j++) {
          int stat = 0;
          for (int k = (int)1; k <= (int)n; k++)
            stat |= (A[k][i] ^ j) * (1 << (k - 1));
          f[cnt][stat]++;
        }
      }
    }
    return cnt;
  }
  int x = 0;
  for (int i = l; i <= r; i++) {
    if (s[i] == '(') {
      i = V[i];
      continue;
    }
    if (s[i] == '&' || s[i] == '|' || s[i] == '?') {
      x = i;
      break;
    }
  }
  if (!x) return work(l + 1, r - 1);
  int lx = work(l, x - 1);
  int rx = work(x + 1, r);
  ++cnt;
  if (s[x] != '|') {
    And(cnt, lx, rx);
  }
  if (s[x] != '&') {
    Or(cnt, lx, rx);
  }
  return cnt;
}
int main() {
  scanf("%s", s + 1);
  scanf("%d", &n);
  int qu = 0;
  U = 1 << n;
  for (int i = (int)1; i <= (int)n; i++) {
    for (int j = (int)1; j <= (int)4; j++) scanf("%d", &A[i][j]);
    scanf("%d", res + i);
    if (res[i]) qu |= 1 << (i - 1);
  }
  l = strlen(s + 1);
  for (int i = (int)1; i <= (int)l; i++) {
    if (s[i] == '(') stk[++top] = i;
    if (s[i] == ')') {
      V[stk[top]] = i;
      top--;
    }
  }
  int x = work(1, l);
  printf("%d\n", f[x][qu]);
  return 0;
}
