#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, width = 16, len = 1 << width;
char expr[505];
namespace FWT {
void fwt_or(int* a, int type) {
  for (int l = 2; l <= len; l <<= 1)
    for (int *p = a, m = l >> 1; p != a + len; p += l)
      for (int i = 0; i < m; i++) {
        int &x = p[i], &y = p[i + m];
        if (type == 1)
          y = (y + x) % mod;
        else
          y = (y - x) % mod;
      }
}
void fwt_and(int* a, int type) {
  for (int l = 2; l <= len; l <<= 1)
    for (int *p = a, m = l >> 1; p != a + len; p += l)
      for (int i = 0; i < m; i++) {
        int &x = p[i], &y = p[i + m];
        if (type == 1)
          x = (x + y) % mod;
        else
          x = (x - y) % mod;
      }
}
void mul_or(int* a, int* b, int* c) {
  static int A[len], B[len];
  copy(a, a + len, A);
  copy(b, b + len, B);
  fwt_or(A, 1);
  fwt_or(B, 1);
  for (int i = 0; i < len; i++) A[i] = 1ll * A[i] * B[i] % mod;
  fwt_or(A, -1);
  for (int i = 0; i < len; i++) c[i] = (c[i] + A[i]) % mod;
}
void mul_and(int* a, int* b, int* c) {
  static int A[len], B[len];
  copy(a, a + len, A);
  copy(b, b + len, B);
  fwt_and(A, 1);
  fwt_and(B, 1);
  for (int i = 0; i < len; i++) A[i] = 1ll * A[i] * B[i] % mod;
  fwt_and(A, -1);
  for (int i = 0; i < len; i++) c[i] = (c[i] + A[i]) % mod;
}
}  // namespace FWT
void dbg(int* f, int state) {
  if (!f[state]) return;
  for (int i = 0; i < width; i++) {
    int A = i & 1, B = i >> 1 & 1, C = i >> 2 & 1, D = i >> 3 & 1;
    cout << "\tA=" << A << " B=" << B << " C=" << C << " D=" << D
         << " val=" << (state >> i & 1) << " i=" << i << "\n";
  }
  cout << "way=" << f[state] << "\n";
}
int* dfs(int l, int r) {
  if (l == r) {
    int* f = new int[len];
    fill(f, f + len, 0);
    auto add = [&](int x, int b) {
      int state = 0;
      for (int i = 0; i < 16; i++) state |= ((i >> x & 1) == b) << i;
      f[state]++;
    };
    if (isupper(expr[l]))
      add(expr[l] - 'A', 1);
    else if (islower(expr[l]))
      add(expr[l] - 'a', 0);
    else
      for (int i = 0; i < 4; i++)
        for (int j = 0; j < 2; j++) add(i, j);
    return f;
  }
  int cnt = 0;
  for (int i = l; i <= r; i++) {
    if (expr[i] == '(') cnt++;
    if (cnt == 0) {
      int* L = dfs(l + 1, i - 2);
      int* R = dfs(i + 2, r - 1);
      int* f = new int[len];
      fill(f, f + len, 0);
      if (expr[i] == '&' || expr[i] == '?') FWT::mul_and(L, R, f);
      if (expr[i] == '|' || expr[i] == '?') FWT::mul_or(L, R, f);
      delete[] L;
      delete[] R;
      return f;
    }
    if (expr[i] == ')') cnt--;
  }
  throw;
}
int main() {
  cin >> expr;
  int *res = dfs(0, strlen(expr) - 1), n, mask = 0, result = 0, ans = 0;
  cin >> n;
  pair<int, int> limit[16];
  for (int i = 0; i < n; i++) {
    int A, B, C, D, f, id = 0;
    cin >> A >> B >> C >> D >> f;
    id |= A << 0;
    id |= B << 1;
    id |= C << 2;
    id |= D << 3;
    limit[i] = make_pair(id, f);
    mask |= 1 << id;
    result |= f << id;
  }
  for (int i = 0; i < len; i++) {
    if ((i & mask) == result) {
      ans = (ans + res[i]) % mod;
    }
  }
  cout << (ans + mod) % mod << "\n";
  return 0;
}
