#include <bits/stdc++.h>
using namespace std;
int f[505 / 2][1 << 16], a[1 << 16], b[1 << 16], c[1 << 16], len = 0;
int n, m, son[505][2], A[16], B[16], C[16], D[16], E[16], rt, T;
char st0[505], st[505];
int build(int l, int r) {
  int i, s = 0, p = 0, q;
  if (l == r) {
    st[++len] = st0[l];
    return len;
  }
  for (i = l; i < r; i++) {
    s += (st0[i] == '(');
    s -= (st0[i] == ')');
    if (!s) break;
  }
  p = i + 1, st[++len] = st0[p], q = len;
  son[q][0] = build(l + 1, p - 2);
  son[q][1] = build(p + 2, r - 1);
  return q;
}
void update_val(int t, char c) {
  int i, S = 0;
  for (i = 0; i < m; i++) {
    if (c == 'A') S += (A[i] << i);
    if (c == 'B') S += (B[i] << i);
    if (c == 'C') S += (C[i] << i);
    if (c == 'D') S += (D[i] << i);
    if (c == 'a') S += ((A[i] ^ 1) << i);
    if (c == 'b') S += ((B[i] ^ 1) << i);
    if (c == 'c') S += ((C[i] ^ 1) << i);
    if (c == 'd') S += ((D[i] ^ 1) << i);
  }
  f[t][S]++;
}
void FWT1(int *x, int l, int r, int op) {
  int i, mid = (l + r) >> 1;
  if (l == r) return;
  FWT1(x, l, mid, op);
  FWT1(x, mid + 1, r, op);
  for (i = l; i <= mid; i++) (x[i] += op * x[mid + i - l + 1]) %= 1000000007;
}
void FWT2(int *x, int l, int r, int op) {
  int i, mid = (l + r) >> 1;
  if (l == r) return;
  FWT2(x, l, mid, op);
  FWT2(x, mid + 1, r, op);
  for (i = mid + 1; i <= r; i++)
    (x[i] += op * x[l - 1 + i - mid]) %= 1000000007;
}
void FFT1(int t, int u, int v) {
  int i;
  for (i = 0; i < (1 << m); i++) a[i] = f[u][i];
  for (i = 0; i < (1 << m); i++) b[i] = f[v][i];
  FWT1(a, 0, (1 << m) - 1, 1);
  FWT1(b, 0, (1 << m) - 1, 1);
  for (i = 0; i < (1 << m); i++) c[i] = 1LL * a[i] * b[i] % 1000000007;
  FWT1(c, 0, (1 << m) - 1, -1);
  for (i = 0; i < (1 << m); i++) (f[t][i] += c[i]) %= 1000000007;
}
void FFT2(int t, int u, int v) {
  int i;
  for (i = 0; i < (1 << m); i++) a[i] = f[u][i];
  for (i = 0; i < (1 << m); i++) b[i] = f[v][i];
  FWT2(a, 0, (1 << m) - 1, 1);
  FWT2(b, 0, (1 << m) - 1, 1);
  for (i = 0; i < (1 << m); i++) c[i] = 1LL * a[i] * b[i] % 1000000007;
  FWT2(c, 0, (1 << m) - 1, -1);
  for (i = 0; i < (1 << m); i++) (f[t][i] += c[i]) %= 1000000007;
}
void update_sym(int t, char c) {
  if (c == '&') FFT1(t, (son[t][0]), (son[t][1]));
  if (c == '|') FFT2(t, (son[t][0]), (son[t][1]));
}
void dp(int t) {
  if (!son[t][0] && !son[t][1]) {
    if (st[t] == '?') {
      update_val(t, 'A');
      update_val(t, 'a');
      update_val(t, 'B');
      update_val(t, 'b');
      update_val(t, 'C');
      update_val(t, 'c');
      update_val(t, 'D');
      update_val(t, 'd');
    } else
      update_val(t, st[t]);
    return;
  }
  dp(son[t][0]);
  dp(son[t][1]);
  if (st[t] == '?') {
    update_sym(t, '&');
    update_sym(t, '|');
  } else
    update_sym(t, st[t]);
}
int main() {
  int i;
  scanf("%s", st0 + 1), n = strlen(st0 + 1);
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d %d %d %d %d", &A[i], &B[i], &C[i], &D[i], &E[i]);
    T |= (1 << i) * E[i];
  }
  rt = build(1, n);
  dp(rt);
  printf("%d", (f[rt][T] + 1000000007) % 1000000007);
  return 0;
}
