#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7;
const int N = 505;
char str[N];
int cnt;
int f[N][1 << 16];
void updata(int &x, int y) {
  x += y;
  if (x >= P) x -= P;
}
void gett(int ch, int f[]) {
  int pos, x;
  if (isupper(ch)) {
    pos = ch - 'A', x = 1;
  } else {
    pos = ch - 'a', x = 0;
  }
  int opt = 0;
  for (int mask = 0; mask < 1 << 4; ++mask)
    if ((mask >> pos & 1) == x) opt |= 1 << mask;
  updata(f[opt], 1);
}
int g1[1 << 16], g2[1 << 16], g3[1 << 16];
void fwtor(int f1[], int f2[], int f3[]) {
  for (int i = 0; i < 1 << 16; ++i) {
    g1[i] = f1[i];
    g2[i] = f2[i];
  }
  for (int i = 0; i < 16; ++i)
    for (int j = 0; j < 1 << 16; ++j)
      if (!(j >> i & 1)) {
        updata(g1[j | (1 << i)], g1[j]);
        updata(g2[j | (1 << i)], g2[j]);
      }
  for (int i = 0; i < 1 << 16; ++i) g3[i] = (long long)g1[i] * g2[i] % P;
  for (int i = 0; i < 16; ++i)
    for (int j = 0; j < 1 << 16; ++j)
      if ((j >> i & 1)) {
        updata(g3[j], P - g3[j ^ (1 << i)]);
      }
  for (int i = 0; i < 1 << 16; ++i) updata(f3[i], g3[i]);
}
void fwtand(int f1[], int f2[], int f3[]) {
  for (int i = 0; i < 1 << 16; ++i) {
    g1[i] = f1[i];
    g2[i] = f2[i];
  }
  for (int i = 0; i < 16; ++i)
    for (int j = 0; j < 1 << 16; ++j)
      if (!(j >> i & 1)) {
        updata(g1[j], g1[j | (1 << i)]);
        updata(g2[j], g2[j | (1 << i)]);
      }
  for (int i = 0; i < 1 << 16; ++i) g3[i] = (long long)g1[i] * g2[i] % P;
  for (int i = 0; i < 16; ++i)
    for (int j = 0; j < 1 << 16; ++j)
      if ((j >> i & 1)) {
        updata(g3[j ^ (1 << i)], P - g3[j]);
      }
  for (int i = 0; i < 1 << 16; ++i) updata(f3[i], g3[i]);
}
int solve(int l, int r) {
  int Q;
  int flag = 0;
  if (l == r) {
    Q = ++cnt;
    if (str[l] != '?') {
      gett(str[l], f[Q]);
    } else {
      for (int i = 0; i < 4; ++i) {
        gett('a' + i, f[Q]);
        gett('A' + i, f[Q]);
      }
    }
    flag = 1;
  } else {
    int sum = 0;
    for (int i = l; i <= r; ++i) {
      if (str[i] == '(')
        ++sum;
      else if (str[i] == ')')
        --sum;
      else if (sum == 0) {
        Q = ++cnt;
        flag = 1;
        int z1 = solve(l, i - 1);
        int z2 = solve(i + 1, r);
        if (str[i] != '&') fwtor(f[z1], f[z2], f[Q]);
        if (str[i] != '|') fwtand(f[z1], f[z2], f[Q]);
        break;
      }
    }
  }
  if (flag == 0 && str[l] == '(' && str[r] == ')') Q = solve(l + 1, r - 1);
  return Q;
}
int main() {
  scanf("%s", str);
  int m = solve(0, strlen(str) - 1);
  int n;
  scanf("%d", &n);
  int opt1 = 0, opt2 = 0;
  for (int i = 1; i <= n; ++i) {
    int mask = 0;
    for (int j = 0; j < 4; ++j) {
      int a;
      scanf("%d", &a);
      mask |= a << j;
    }
    int q;
    scanf("%d", &q);
    opt1 |= 1 << mask;
    opt2 |= q << mask;
  }
  int ans = 0;
  for (int mask = 0; mask < 1 << 16; ++mask)
    if ((mask & opt1) == opt2) updata(ans, f[m][mask]);
  printf("%d\n", ans);
  return 0;
}
