#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool d = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') d = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return d ? x : -x;
}
inline unsigned long long rnd() {
  return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4;
}
const int N = (1 << 16) + 5, M = 505, mo = 1e9 + 7;
int st[20][N], tot, lson[M], rson[M], alb[N], a[M];
char s[M], ch[M];
int MN(int x, int y) { return a[x] < a[y] ? x : y; }
int find(int l, int r) {
  int k = alb[r - l + 1];
  return MN(st[k][l], st[k][r - (1 << k) + 1]);
}
int build(int l, int r) {
  if (l > r) return 0;
  int pos = find(l, r);
  if (s[pos] == '(' || s[pos] == ')') return 0;
  int rt = ++tot;
  ch[rt] = s[pos];
  lson[rt] = build(l, pos - 1);
  rson[rt] = build(pos + 1, r);
  return rt;
}
int b[N][5], m, dp[M / 2][N], f[N], g[N], h[N];
void FMT(int *a, bool d) {
  if (!d) {
    for (int i = (int)(0); i <= (int)(m - 1); i++) {
      for (int j = (int)(0); j <= (int)((1 << m) - 1); j++)
        if (j >> i & 1) {
          a[j] = (a[j] + a[j ^ (1 << i)]) % mo;
        }
    }
  } else {
    for (int i = (int)(0); i <= (int)(m - 1); i++) {
      for (int j = (int)((1 << m) - 1); j >= (int)(0); j--)
        if (j >> i & 1) {
          a[j ^ (1 << i)] = (a[j ^ (1 << i)] + a[j]) % mo;
        }
    }
  }
}
void IFMT(int *a, bool d) {
  if (!d) {
    for (int i = (int)(0); i <= (int)(m - 1); i++) {
      for (int j = (int)(0); j <= (int)((1 << m) - 1); j++)
        if (j >> i & 1) {
          a[j] = (a[j] - a[j ^ (1 << i)] + mo) % mo;
        }
    }
  } else {
    for (int i = (int)(0); i <= (int)(m - 1); i++) {
      for (int j = (int)((1 << m) - 1); j >= (int)(0); j--)
        if (j >> i & 1) {
          a[j ^ (1 << i)] = (a[j ^ (1 << i)] - a[j] + mo) % mo;
        }
    }
  }
}
void solve(int u, bool d) {
  for (int i = (int)(0); i <= (int)((1 << m) - 1); i++) f[i] = dp[lson[u]][i];
  for (int i = (int)(0); i <= (int)((1 << m) - 1); i++) g[i] = dp[rson[u]][i];
  FMT(f, d);
  FMT(g, d);
  for (int i = (int)(0); i <= (int)((1 << m) - 1); i++)
    h[i] = (long long)f[i] * g[i] % mo;
  IFMT(h, d);
}
void dfs(int u) {
  if (u == 0) return;
  if (!lson[u] && !rson[u]) {
    if (ch[u] == '?') {
      for (int i = (int)(0); i <= (int)(3); i++) {
        int st = 0, ST = 0;
        for (int j = (int)(0); j <= (int)(m - 1); j++)
          st |= b[j][i] << j, ST |= (b[j][i] ^ 1) << j;
        dp[u][st]++;
        dp[u][ST]++;
      }
    } else {
      int st = 0, d = (ch[u] >= 'a' && ch[u] <= 'd');
      for (int i = (int)(0); i <= (int)(m - 1); i++) {
        int p = (d ? ch[u] - 'a' : ch[u] - 'A');
        st |= (b[i][p] ^ d) << i;
      }
      dp[u][st] = 1;
    }
    return;
  }
  dfs(lson[u]);
  dfs(rson[u]);
  if (ch[u] != '?') {
    int p = (ch[u] == '|' ? 0 : 1);
    solve(u, p);
    for (int i = (int)(0); i <= (int)((1 << m) - 1); i++) dp[u][i] = h[i];
  } else {
    solve(u, 0);
    for (int i = (int)(0); i <= (int)((1 << m) - 1); i++) dp[u][i] = h[i];
    solve(u, 1);
    for (int i = (int)(0); i <= (int)((1 << m) - 1); i++)
      dp[u][i] = (dp[u][i] + h[i]) % mo;
  }
}
int main() {
  for (int i = (int)(2); i <= (int)(N - 1); i++) alb[i] = alb[i >> 1] + 1;
  scanf("%s", s + 1);
  int n = strlen(s + 1), top = 0;
  for (int i = (int)(1); i <= (int)(n); i++) {
    if (s[i] != '(' && s[i] != ')')
      a[i] = top;
    else {
      a[i] = 0x3f3f3f3f;
      if (s[i] == '(')
        top++;
      else
        top--;
    }
    st[0][i] = i;
  }
  for (int i = (int)(1); i <= (int)(10); i++) {
    for (int j = 1; j + (1 << i) - 1 <= n; j++) {
      st[i][j] = MN(st[i - 1][j], st[i - 1][j + (1 << i - 1)]);
    }
  }
  build(1, n);
  m = read();
  for (int i = (int)(0); i <= (int)(m - 1); i++)
    for (int j = (int)(0); j <= (int)(4); j++) b[i][j] = read();
  dfs(1);
  int st = 0;
  for (int i = (int)(0); i <= (int)(m - 1); i++) st |= b[i][4] << i;
  cout << dp[1][st];
}
