#include <bits/stdc++.h>
using namespace std;
inline int M(int x) { return x >= 1000000007 ? x - 1000000007 : x; }
inline int M1(int x) { return x < 0 ? x + 1000000007 : x; }
int n, m, root, cn = 0, ans = 0, f[251][65537];
char s[527];
inline void Add(int x, char c) {
  int i, j, p;
  for (i = 0; i < 4; ++i) {
    if (c == ('A' + i)) {
      p = 0;
      for (j = 0; j < 16; j++)
        if ((j >> i) & 1) p |= (1 << j);
      ++f[x][p];
      break;
    }
    if (c == ('a' + i)) {
      p = 0;
      for (j = 0; j < 16; j++)
        if (!((j >> i) & 1)) p |= (1 << j);
      ++f[x][p];
      break;
    }
  }
}
inline void OMT(int f[]) {
  for (int i = 0; i < 16; ++i)
    for (int j = 0; j < 65536; ++j)
      if (j & (1 << i)) f[j] = M(f[j] + f[j ^ (1 << i)]);
}
inline void OMI(int f[]) {
  for (int i = 0; i < 16; ++i)
    for (int j = 0; j < 65536; ++j)
      if (j & (1 << i)) f[j] = M1(f[j] - f[j ^ (1 << i)]);
}
inline void AMT(int f[]) {
  for (int i = 0; i < 16; ++i)
    for (int j = 0; j < 65536; ++j)
      if (!(j & (1 << i))) f[j] = M(f[j] + f[j | (1 << i)]);
}
inline void AMI(int f[]) {
  for (int i = 0; i < 16; ++i)
    for (int j = 0; j < 65536; ++j)
      if (!(j & (1 << i))) f[j] = M1(f[j] - f[j | (1 << i)]);
}
int tmp[65537], t1[65537], t2[65537];
inline void Tran(int x, int ls, int rs, char c) {
  int i, *p = f[x];
  memcpy(t1, f[ls], sizeof(f[ls]));
  memcpy(t2, f[rs], sizeof(f[rs]));
  if (c == '&') {
    AMT(t1);
    AMT(t2);
    for (i = 0; i < 65536; i++) tmp[i] = 1ll * t1[i] * t2[i] % 1000000007;
    AMI(tmp);
    for (i = 0; i < 65536; i++) p[i] = M(p[i] + tmp[i]);
  }
  if (c == '|') {
    OMT(t1);
    OMT(t2);
    for (i = 0; i < 65536; i++) tmp[i] = 1ll * t1[i] * t2[i] % 1000000007;
    OMI(tmp);
    for (i = 0; i < 65536; i++) p[i] = M(p[i] + tmp[i]);
  }
}
int Dich(int l, int r) {
  int x = ++cn;
  if (l == r) {
    if (s[l] == '?')
      for (int i = 0; i < 4; ++i) {
        Add(x, 'A' + i);
        Add(x, 'a' + i);
      }
    else
      Add(x, s[l]);
    return x;
  }
  int i, mid = 0, sum = 0, ls, rs;
  for (i = l; i <= r; i++) {
    if (s[i] == '(') ++sum;
    if (s[i] == ')') --sum;
    if (!sum) {
      mid = i + 1;
      break;
    }
  }
  ls = Dich(l + 1, mid - 2);
  rs = Dich(mid + 2, r - 1);
  if (s[mid] == '?') {
    Tran(x, ls, rs, '&');
    Tran(x, ls, rs, '|');
  } else
    Tran(x, ls, rs, s[mid]);
  return x;
}
int sta[17], V[17];
inline bool can(int st) {
  for (int i = 1; i <= m; i++)
    if (((st >> sta[i]) & 1) != V[i]) return false;
  return true;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  root = Dich(1, n);
  int i, A, B, C, D;
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d%d%d%d", &A, &B, &C, &D, &V[i]);
    sta[i] = ((A << 0) | (B << 1) | (C << 2) | (D << 3));
  }
  for (i = 0; i < 65536; i++)
    if (can(i)) ans = M(ans + f[root][i]);
  printf("%d", ans);
  return 0;
}
