#include <bits/stdc++.h>
using namespace std;
int n;
char d[512];
int c[512][65536], t[4][65536];
map<char, int> init;
int L;
int lim[16][2];
void C(int s, int e) {
  if (s < e) {
    int m, st;
    st = 1;
    for (m = s + 1; m < e; m++) {
      if (d[m] == '(') st++;
      if (d[m] == ')') st--;
      if (st == 0) break;
    }
    m++;
    C(s + 1, m - 2);
    C(m + 2, e - 1);
    for (int i = 0; i < 65536; i++) {
      t[0][i] = c[s + 1][i];
      t[1][i] = c[m + 2][i];
      t[2][i] = c[s + 1][65536 - 1 - i];
      t[3][i] = c[m + 2][65536 - 1 - i];
    }
    for (int i = 0; i < 16; i++) {
      int k = 1 << i;
      for (int j = 0; j < 65536; j++) {
        if (j & k) {
          for (int l = 0; l < 4; l++) {
            t[l][j] += t[l][j - k];
            if (t[l][j] >= 1000000007) t[l][j] -= 1000000007;
          }
        }
      }
    }
    for (int i = 0; i < 65536; i++) {
      t[0][i] = (long long)t[0][i] * t[1][i] % 1000000007;
      t[2][i] = (long long)t[2][i] * t[3][i] % 1000000007;
    }
    for (int i = 0; i < 16; i++) {
      int k = 1 << i;
      for (int j = 0; j < 65536; j++) {
        if (j & k) {
          t[0][j] -= t[0][j - k];
          if (t[0][j] < 0) t[0][j] += 1000000007;
          t[2][j] -= t[2][j - k];
          if (t[2][j] < 0) t[2][j] += 1000000007;
        }
      }
    }
    if (d[m] == '|' || d[m] == '?') {
      for (int i = 0; i < 65536; i++) {
        c[s][i] += t[0][i];
        if (c[s][i] >= 1000000007) c[s][i] -= 1000000007;
      }
    }
    if (d[m] == '&' || d[m] == '?') {
      for (int i = 0; i < 65536; i++) {
        c[s][i] += t[2][65536 - 1 - i];
        if (c[s][i] >= 1000000007) c[s][i] -= 1000000007;
      }
    }
  } else {
    if (d[s] == '?') {
      for (auto i : init) c[s][i.second] = 1;
    } else
      c[s][init[d[s]]] = 1;
  }
}
int main() {
  scanf("%s", d);
  for (n = 0; d[n]; n++)
    ;
  for (int i = 0; i < 4; i++) {
    int k = 0;
    for (int j = 0; j < 16; j++) {
      if (j & (1 << i)) k += 1 << j;
    }
    init['A' + i] = k;
    k = 0;
    for (int j = 0; j < 16; j++) {
      if ((j & (1 << i)) == 0) k += 1 << j;
    }
    init['a' + i] = k;
  }
  C(0, n - 1);
  scanf("%d", &L);
  for (int i = 0; i < L; i++) {
    for (int j = 0; j < 4; j++) {
      int k;
      scanf("%d", &k);
      lim[i][0] += (1 << j) * k;
    }
    scanf("%d", &lim[i][1]);
  }
  int r = 0;
  for (int i = 0; i < 65536; i++) {
    int j;
    for (j = 0; j < L; j++) {
      if (((i & (1 << lim[j][0])) > 0) != lim[j][1]) break;
    }
    if (j >= L) {
      r += c[0][i];
      if (r >= 1000000007) r -= 1000000007;
    }
  }
  printf("%d", r);
  return 0;
}
