#include <bits/stdc++.h>
template <typename A, typename B>
inline char smax(A &a, const B &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename A, typename B>
inline char smin(A &a, const B &b) {
  return b < a ? a = b, 1 : 0;
}
template <typename I>
inline void read(I &x) {
  int f = 0, c;
  while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
  x = c & 15;
  while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
  f ? x = -x : 0;
}
const int N = 500 + 7;
const int M = (1 << 16) + 7;
const int P = 1e9 + 7;
int n, m, S, nod;
int A, B, C, D, F;
int fa[N], lc[N], rc[N], v[N], dp[N][M];
char a[N];
inline void sadd(int &x, const int &y) {
  x += y;
  x >= P ? x -= P : x;
}
inline int smod(const int &x) { return x >= P ? x - P : x; }
inline int fpow(int x, int y) {
  int ans = 1;
  for (; y; y >>= 1, x = (long long)x * x % P)
    if (y & 1) ans = (long long)ans * x % P;
  return ans;
}
inline void fwt_and(int *a, int n, int f) {
  for (int i = 1; i < n; i <<= 1)
    for (int j = 0; j < n; j += i << 1)
      for (int k = 0; k < i; ++k) {
        int x = a[j + k], y = a[i + j + k];
        if (f > 0)
          a[j + k] = smod(x + y);
        else
          a[j + k] = smod(x + P - y);
      }
}
inline void fwt_or(int *a, int n, int f) {
  for (int i = 1; i < n; i <<= 1)
    for (int j = 0; j < n; j += i << 1)
      for (int k = 0; k < i; ++k) {
        int x = a[j + k], y = a[i + j + k];
        if (f > 0)
          a[i + j + k] = smod(x + y);
        else
          a[i + j + k] = smod(y + P - x);
      }
}
int tA[M], tB[M];
inline void mul_and(int *a, int *b, int *c) {
  memcpy(tA, a, sizeof(*a) * S);
  memcpy(tB, b, sizeof(*b) * S);
  fwt_and(tA, S, 1), fwt_and(tB, S, 1);
  for (int i = 0; i < S; ++i) tA[i] = (long long)tA[i] * tB[i] % P;
  fwt_and(tA, S, -1);
  for (int i = 0; i < S; ++i) sadd(c[i], tA[i]);
}
inline void mul_or(int *a, int *b, int *c) {
  memcpy(tA, a, sizeof(*a) * S);
  memcpy(tB, b, sizeof(*b) * S);
  fwt_or(tA, S, 1), fwt_or(tB, S, 1);
  for (int i = 0; i < S; ++i) tA[i] = (long long)tA[i] * tB[i] % P;
  fwt_or(tA, S, -1);
  for (int i = 0; i < S; ++i) sadd(c[i], tA[i]);
}
inline void dfs(int x, int fa = 0) {
  if (!lc[x]) {
    if (v[x] == 1)
      dp[x][A] = 1;
    else if (v[x] == 2)
      dp[x][B] = 1;
    else if (v[x] == 3)
      dp[x][C] = 1;
    else if (v[x] == 4)
      dp[x][D] = 1;
    else if (v[x] == 5)
      dp[x][(S - 1) ^ A] = 1;
    else if (v[x] == 6)
      dp[x][(S - 1) ^ B] = 1;
    else if (v[x] == 7)
      dp[x][(S - 1) ^ C] = 1;
    else if (v[x] == 8)
      dp[x][(S - 1) ^ D] = 1;
    else if (v[x] == 0)
      ++dp[x][A], ++dp[x][(S - 1) ^ A], ++dp[x][B], ++dp[x][(S - 1) ^ B],
          ++dp[x][C], ++dp[x][(S - 1) ^ C], ++dp[x][D], ++dp[x][(S - 1) ^ D];
  } else {
    dfs(lc[x], x), dfs(rc[x], x);
    if (v[x] == -1 || v[x] == 0) mul_and(dp[lc[x]], dp[rc[x]], dp[x]);
    if (v[x] == -2 || v[x] == 0) mul_or(dp[lc[x]], dp[rc[x]], dp[x]);
  }
}
inline void build() {
  int curs = nod = 1;
  n = strlen(a + 1);
  for (int i = 1; i <= n; ++i) {
    if (a[i] == '(')
      fa[++nod] = curs, curs = nod;
    else if (a[i] == ')') {
      if (lc[fa[curs]])
        rc[fa[curs]] = curs;
      else
        lc[fa[curs]] = curs;
      curs = fa[curs];
    } else if (a[i] >= 'A' && a[i] <= 'D')
      v[curs] = a[i] - 'A' + 1;
    else if (a[i] >= 'a' && a[i] <= 'd')
      v[curs] = a[i] - 'a' + 5;
    else if (a[i] == '&')
      v[curs] = -1;
    else if (a[i] == '|')
      v[curs] = -2;
  }
}
inline void work() {
  build();
  dfs(1);
  printf("%d\n", dp[1][F]);
}
inline void init() {
  scanf("%s", a + 1);
  read(m), S = 1 << m;
  int x;
  for (int i = 1; i <= m; ++i) {
    read(x), x && (A |= 1 << (i - 1));
    read(x), x && (B |= 1 << (i - 1));
    read(x), x && (C |= 1 << (i - 1));
    read(x), x && (D |= 1 << (i - 1));
    read(x), x && (F |= 1 << (i - 1));
  }
}
int main() {
  init();
  work();
  fclose(stdin), fclose(stdout);
  return 0;
}
