#include <bits/stdc++.h>
using namespace std;
const int N = 510;
const int P = 1e9 + 7;
const int inv = 5e8 + 4;
const int M = 1 << 16;
char s[N];
int g[16], f[N][M], n, cnt, tmp1[M], tmp2[M];
void FWT(int* a, int n, int p) {
  for (int d = 1; d < n; d <<= 1)
    for (int m = d << 1, i = 0; i < n; i += m)
      for (int j = 0; j < d; j++) {
        int x = a[i + j], y = a[i + j + d];
        if (p == 0) {
          a[i + j] = (x + y) % P;
        } else if (p == 1) {
          a[i + j + d] = (x + y) % P;
        }
      }
}
void UFWT(int* a, int n, int p) {
  for (int d = 1; d < n; d <<= 1)
    for (int m = d << 1, i = 0; i < n; i += m)
      for (int j = 0; j < d; j++) {
        int x = a[i + j], y = a[i + j + d];
        if (p == 0) {
          a[i + j] = (x - y) % P;
        } else if (p == 1) {
          a[i + j + d] = (y - x) % P;
        }
      }
}
void work2(int tmp, int now) {
  int t, t2;
  switch (s[tmp]) {
    case 'A':
    case 'a':
      t = 0;
      break;
    case 'B':
    case 'b':
      t = 1;
      break;
    case 'C':
    case 'c':
      t = 2;
      break;
    case 'D':
    case 'd':
      t = 3;
      break;
  }
  if (s[tmp] >= 'A' && s[tmp] <= 'D')
    t2 = 0;
  else
    t2 = 1;
  bool pd;
  for (int i = 0; i < M; i++) {
    pd = true;
    for (int j = 0; j < 16; j++) {
      if ((j >> t & 1 ^ t2) != (i >> j & 1)) {
        pd = false;
        break;
      }
    }
    if (pd) f[now][i]++;
  }
}
void work(int l, int r, int now) {
  int tmp = 0;
  if (l == r) {
    tmp = l;
    if (s[tmp] == '?') {
      s[tmp] = 'A', work2(tmp, now);
      s[tmp] = 'B', work2(tmp, now);
      s[tmp] = 'C', work2(tmp, now);
      s[tmp] = 'D', work2(tmp, now);
      s[tmp] = 'a', work2(tmp, now);
      s[tmp] = 'b', work2(tmp, now);
      s[tmp] = 'c', work2(tmp, now);
      s[tmp] = 'd', work2(tmp, now);
    } else {
      work2(tmp, now);
    }
    return;
  }
  int ls, rs, t;
  for (int i = l; i <= r; i++) {
    if (s[i] == '(')
      tmp++;
    else if (s[i] == ')')
      tmp--;
    else if (tmp == 0) {
      t = i;
      work(l + 1, i - 2, ls = ++cnt);
      work(i + 2, r - 1, rs = ++cnt);
      break;
    }
  }
  if (s[t] == '&' || s[t] == '?') {
    FWT(f[ls], M, 0), FWT(f[rs], M, 0);
    for (int i = 0; i < M; i++) f[now][i] = 1LL * f[ls][i] * f[rs][i] % P;
    UFWT(f[now], M, 0);
    memcpy(tmp1, f[now], sizeof(tmp1));
  }
  if (s[t] == '|' || s[t] == '?') {
    if (s[t] == '?') {
      UFWT(f[ls], M, 0), UFWT(f[rs], M, 0);
    }
    FWT(f[ls], M, 1), FWT(f[rs], M, 1);
    for (int i = 0; i < M; i++) f[now][i] = 1LL * f[ls][i] * f[rs][i] % P;
    UFWT(f[now], M, 1);
    memcpy(tmp2, f[now], sizeof(tmp2));
  }
  if (s[t] == '?') {
    for (int i = 0; i < M; i++) f[now][i] = (tmp1[i] + tmp2[i]) % P;
  }
}
int main() {
  scanf("%s", s);
  scanf("%d", &n);
  for (int i = 15; i >= 0; i--) g[i] = -1;
  for (int i = 0; i < n; i++) {
    int a, b, c, d, e;
    scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
    g[a + (b << 1) + (c << 2) + (d << 3)] = e;
  }
  cnt = 1;
  work(0, strlen(s) - 1, 1);
  int ans = 0;
  bool pd;
  for (int i = 0; i < M; i++) {
    pd = true;
    for (int j = 0; j < 16; j++) {
      if (g[j] != -1 && g[j] != (i >> j & 1)) {
        pd = false;
        break;
      }
    }
    if (pd) ans = (ans + f[1][i]) % P;
  }
  ans = (ans % P + P) % P;
  printf("%d\n", ans);
  return 0;
}
