#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool checkMax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T, typename... Args>
inline void checkMax(T &a, const Args... arg) {
  checkMax(a, max(arg...));
}
template <class T>
inline bool checkMin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename... Args>
inline void checkMin(T &a, const Args... arg) {
  checkMin(a, min(arg...));
}
const int INF = 0x3f3f3f3f;
const long long llINF = 1e18;
const int MAXS = 505;
const int MAXN = 20;
const int MAXM = 1 << 16;
const int MOD = 1e9 + 7;
void addmod(int &x, int y) {
  x += y;
  if (x >= MOD) x -= MOD;
}
void submod(int &x, int y) {
  x -= y;
  if (x < 0) x += MOD;
}
int add(int x, int y) {
  addmod(x, y);
  return x;
}
int sub(int x, int y) {
  submod(x, y);
  return x;
}
int n, m, tot;
int val[MAXN][5], f[MAXS][MAXM], fa[MAXS];
char s[MAXS], c[MAXS];
vector<int> son[MAXS];
void FWTOR(int *a, int n, int flag) {
  for (int i = 2; i <= n; i <<= 1) {
    for (int j = 0; j < n; j += i) {
      for (int k = j; k < j + (i >> 1); k++) {
        if (~flag)
          addmod(a[k + (i >> 1)], a[k]);
        else
          submod(a[k + (i >> 1)], a[k]);
      }
    }
  }
}
void FWTAND(int *a, int n, int flag) {
  for (int i = 2; i <= n; i <<= 1) {
    for (int j = 0; j < n; j += i) {
      for (int k = j; k < j + (i >> 1); k++) {
        if (~flag)
          addmod(a[k], a[k + (i >> 1)]);
        else
          submod(a[k], a[k + (i >> 1)]);
      }
    }
  }
}
void MulOR(int *A, int *B, int *C) {
  static int a[MAXM], b[MAXM], c[MAXM];
  for (int i = 0; i < m; i++) a[i] = A[i], b[i] = B[i];
  FWTOR(a, m, 1);
  FWTOR(b, m, 1);
  for (int i = 0; i < m; i++) c[i] = 1ll * a[i] * b[i] % MOD;
  FWTOR(c, m, -1);
  for (int i = 0; i < m; i++) addmod(C[i], c[i]);
}
void MulAND(int *A, int *B, int *C) {
  static int a[MAXM], b[MAXM], c[MAXM];
  for (int i = 0; i < m; i++) a[i] = A[i], b[i] = B[i];
  FWTAND(a, m, 1);
  FWTAND(b, m, 1);
  for (int i = 0; i < m; i++) c[i] = 1ll * a[i] * b[i] % MOD;
  FWTAND(c, m, -1);
  for (int i = 0; i < m; i++) addmod(C[i], c[i]);
}
void dfs(int u) {
  if (son[u].empty()) {
    if (c[u] == '?') {
      for (int i = 0; i < 4; i++) {
        int S = 0;
        for (int j = 0; j < n; j++) S |= val[j][i] << j;
        addmod(f[u][S], 1);
        addmod(f[u][(m - 1) ^ S], 1);
      }
    } else {
      int x = c[u] - (c[u] >= 'A' && c[u] <= 'D' ? 'A' : 'a'), S = 0;
      for (int j = 0; j < n; j++) S |= val[j][x] << j;
      addmod(f[u][c[u] >= 'A' && c[u] <= 'D' ? S : ((m - 1) ^ S)], 1);
    }
    return;
  }
  dfs(son[u][0]);
  dfs(son[u][1]);
  if (c[u] != '&') MulOR(f[son[u][0]], f[son[u][1]], f[u]);
  if (c[u] != '|') MulAND(f[son[u][0]], f[son[u][1]], f[u]);
}
int main() {
  scanf("%s%d", s + 1, &n);
  m = (1 << n);
  int len = strlen(s + 1);
  tot = 1;
  for (int i = 1, p = 1; i <= len; i++) {
    if (s[i] == '(')
      son[p].push_back(++tot), fa[tot] = p, p = tot;
    else if (s[i] == ')')
      p = fa[p];
    else
      c[p] = s[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 5; j++) scanf("%d", &val[i][j]);
  }
  dfs(1);
  int S = 0;
  for (int i = 0; i < n; i++) S |= val[i][4] << i;
  printf("%d\n", f[1][S]);
  return 0;
}
