#include <bits/stdc++.h>
using namespace std;
int read() {
  int s = 0, t = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') t = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * t;
}
void upd(int &x, int y) { x += x + y >= 1000000007 ? y - 1000000007 : y; }
void fwt(int *a, int lim, int f) {
  for (int k = 2, mid = 1; k <= lim; k <<= 1, mid <<= 1) {
    for (int i = 0; i < lim; i += k)
      for (int j = 0; j < mid; j++) {
        if (f == 1) upd(a[i + mid + j], a[i + j]);
        if (f == 2) upd(a[i + j], a[i + mid + j]);
        if (f == -1) upd(a[i + mid + j], 1000000007 - a[i + j]);
        if (f == -2) upd(a[i + j], 1000000007 - a[i + mid + j]);
      }
  }
}
int tmp[(1 << 16)];
void mul(int ty, int *a, int *b, int *ans) {
  fwt(a, (1 << 16), ty);
  fwt(b, (1 << 16), ty);
  for (int i = 0; i < (1 << 16); i++) tmp[i] = 1ll * a[i] * b[i] % 1000000007;
  fwt(tmp, (1 << 16), -ty);
  fwt(a, (1 << 16), -ty);
  fwt(b, (1 << 16), -ty);
  for (int i = 0; i < (1 << 16); i++) upd(ans[i], tmp[i]);
}
struct edge {
  int to, lt;
} e[(1 << 16)];
int in[(1 << 16)], cnt, p[(1 << 16)], d[(1 << 16)];
void add(int x, int y) {
  e[++cnt].to = y;
  e[cnt].lt = in[x];
  in[x] = cnt;
  d[x]++;
  e[++cnt].to = x;
  e[cnt].lt = in[y];
  in[y] = cnt;
  d[y]++;
}
int sgn(int x) { return x < 0 ? 1 : 0; }
int f[501][(1 << 16)];
char ch[(1 << 16)];
int stk[(1 << 16)], top, tot, sty[(1 << 16)];
int solve(int l, int r) {
  int x = ++tot;
  if (l == r) {
    int sn = ch[l] >= 'a' && ch[l] <= 'd', id = ch[l] - 'a';
    if (ch[l] == '?') {
      for (id = 0; id < 4; id++)
        for (sn = 0; sn < 2; sn++)
          for (int S = 0; S < (1 << 16); S++) {
            int val = 1;
            for (int i = 0; i < 16; i++)
              if ((S >> i & 1) != (i >> id & 1) ^ sn) {
                val = 0;
                break;
              }
            upd(f[x][S], val);
          }
    } else {
      for (int S = 0; S < (1 << 16); S++) {
        f[x][S] = 1;
        for (int i = 0; i < 16; i++)
          if ((S >> i & 1) != (i >> id & 1) ^ sn) {
            f[x][S] = 0;
            break;
          }
      }
    }
    return x;
  }
  int s1 = 0, s2 = 0, t = 0, i;
  for (i = l; i < r; i++) {
    if (ch[i] == '(')
      t++;
    else if (ch[i] == ')')
      t--;
    if (!t) break;
  }
  s1 = solve(l + 1, i - 1);
  s2 = solve(i + 3, r - 1);
  if (ch[i + 1] != '&') mul(1, f[s1], f[s2], f[x]);
  if (ch[i + 1] != '|') mul(2, f[s1], f[s2], f[x]);
  return x;
}
int fs[(1 << 16)], val[(1 << 16)];
int main() {
  scanf("%s", ch + 1);
  int len = strlen(ch + 1);
  int w = solve(1, len);
  int nn = read();
  for (int i = 1; i <= nn; i++) {
    for (int d = 0; d < 4; d++) fs[i] |= (read() << d);
    val[i] = read();
  }
  int ans = 0;
  for (int i, s = 0; s < (1 << 16); s++) {
    for (i = 1; i <= nn; i++)
      if ((s >> fs[i] & 1) != val[i]) break;
    if (i > nn) upd(ans, f[w][s]);
  }
  printf("%d\n", ans);
  return 0;
}
