#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void rread(T &num) {
  num = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') num = num * 10 + ch - '0', ch = getchar();
  num *= f;
}
const int maxn = 505, N = 1e5 + 5, mod = 1e9 + 7;
char ch[N];
int s[105][4], ed;
int ndtot, n;
int f[maxn][N], t1[N], t2[N], t3[N];
inline void AND(int *a, int *b, int *c, int l, int r) {
  if (l == r) {
    c[l] = (long long)a[l] * b[l] % mod;
    return;
  }
  int s = ((l + r) >> 1) - l + 1;
  for (int(i) = (l); (i) <= (((l + r) >> 1)); (i)++)
    a[i] = (a[i] + a[i + s]) % mod, b[i] = (b[i] + b[i + s]) % mod;
  AND(a, b, c, l, ((l + r) >> 1));
  AND(a, b, c, ((l + r) >> 1) + 1, r);
  for (int(i) = (l); (i) <= (((l + r) >> 1)); (i)++)
    c[i] = (c[i] + mod - c[i + s]) % mod;
}
inline void OR(int *a, int *b, int *c, int l, int r) {
  if (l == r) {
    c[l] = (long long)a[l] * b[l] % mod;
    return;
  }
  int s = ((l + r) >> 1) - l + 1;
  for (int(i) = (((l + r) >> 1) + 1); (i) <= (r); (i)++)
    a[i] = (a[i] + a[i - s]) % mod, b[i] = (b[i] + b[i - s]) % mod;
  OR(a, b, c, l, ((l + r) >> 1));
  OR(a, b, c, ((l + r) >> 1) + 1, r);
  for (int(i) = (((l + r) >> 1) + 1); (i) <= (r); (i)++)
    c[i] = (c[i] + mod - c[i - s]) % mod;
}
inline int solve(int l, int r) {
  int x = ++ndtot;
  if (l == r) {
    if (ch[l] != '?' && ch[l] <= 'D') {
      int t = ch[l] - 'A', st = 0;
      for (int(i) = (0); (i) <= (n - 1); (i)++)
        if (s[i][t]) st |= 1 << i;
      f[x][st] = 1;
    } else if (ch[l] != '?') {
      int t = ch[l] - 'a', st = 0;
      for (int(i) = (0); (i) <= (n - 1); (i)++)
        if (!s[i][t]) st |= 1 << i;
      f[x][st] = 1;
    } else {
      for (int(t) = (0); (t) <= (3); (t)++) {
        int st = 0;
        for (int(i) = (0); (i) <= (n - 1); (i)++)
          if (s[i][t]) st |= 1 << i;
        f[x][st]++;
      }
      for (int(t) = (0); (t) <= (3); (t)++) {
        int st = 0;
        for (int(i) = (0); (i) <= (n - 1); (i)++)
          if (!s[i][t]) st |= 1 << i;
        f[x][st]++;
      }
    }
    return x;
  }
  int num = 0, center = 0;
  for (int(i) = (l); (i) <= (r); (i)++) {
    if (ch[i] == '(')
      num++;
    else if (ch[i] == ')')
      num--;
    else if (!num) {
      center = i;
      break;
    }
  }
  int ls = solve(l + 1, center - 2), rs = solve(center + 2, r - 1);
  if (ch[center] == '|')
    OR(f[ls], f[rs], f[x], 0, (1 << n) - 1);
  else if (ch[center] == '&')
    AND(f[ls], f[rs], f[x], 0, (1 << n) - 1);
  else {
    for (int(i) = (0); (i) <= ((1 << n) - 1); (i)++)
      t1[i] = f[ls][i], t2[i] = f[rs][i], t3[i] = 0;
    AND(t1, t2, t3, 0, (1 << n) - 1);
    OR(f[ls], f[rs], f[x], 0, (1 << n) - 1);
    for (int(i) = (0); (i) <= ((1 << n) - 1); (i)++)
      f[x][i] = (f[x][i] + t3[i]) % mod;
  }
  return x;
}
int main() {
  scanf("%s", ch);
  int len = strlen(ch);
  rread(n);
  for (int(i) = (0); (i) <= (n - 1); (i)++) {
    for (int(j) = (0); (j) <= (3); (j)++) rread(s[i][j]);
    int x;
    rread(x);
    if (x) ed |= 1 << i;
  }
  int x = solve(0, len - 1);
  printf("%d", f[x][ed]);
  return 0;
}
