#include <bits/stdc++.h>
using namespace std;
const int VAR_NUM = 4, CASE_NUM = 1 << VAR_NUM, EXPR_NUM = 1 << CASE_NUM;
const int MAX_LEN = 504;
const long long MOD = 1e9 + 7;
template <int d>
void FWT(long long A[EXPR_NUM]) {
  for (int i = 1; i < EXPR_NUM; i *= 2) {
    for (int j = 0; j < EXPR_NUM; j += 2 * i) {
      for (int k = 0; k < i; k++) {
        long long &a = A[j + k], &b = A[j + k + i];
        if (d == 1) {
          (a += b) %= MOD;
        }
        if (d == -1) {
          (a += MOD - b) %= MOD;
        }
        if (d == 2) {
          (b += a) %= MOD;
        }
        if (d == -2) {
          (b += MOD - a) %= MOD;
        }
      }
    }
  }
}
long long stknum[MAX_LEN][EXPR_NUM];
char stkoper[MAX_LEN];
int stknum_n, stkoper_n;
void and_conv(long long A[EXPR_NUM], long long B[EXPR_NUM],
              long long C[EXPR_NUM]) {
  long long tA[EXPR_NUM], tB[EXPR_NUM];
  copy_n(A, EXPR_NUM, tA);
  copy_n(B, EXPR_NUM, tB);
  FWT<1>(tA);
  FWT<1>(tB);
  for (int i = 0; i < EXPR_NUM; i++) (tA[i] *= tB[i]) %= MOD;
  FWT<-1>(tA);
  for (int i = 0; i < EXPR_NUM; i++) (C[i] += tA[i]) %= MOD;
}
void or_conv(long long A[EXPR_NUM], long long B[EXPR_NUM],
             long long C[EXPR_NUM]) {
  long long tA[EXPR_NUM], tB[EXPR_NUM];
  copy_n(A, EXPR_NUM, tA);
  copy_n(B, EXPR_NUM, tB);
  FWT<2>(tA);
  FWT<2>(tB);
  for (int i = 0; i < EXPR_NUM; i++) (tA[i] *= tB[i]) %= MOD;
  FWT<-2>(tA);
  for (int i = 0; i < EXPR_NUM; i++) (C[i] += tA[i]) %= MOD;
}
long long sum_expr(long long A[EXPR_NUM]) {
  long long res = 0;
  for (int i = 0; i < EXPR_NUM; i++) (res += A[i]) %= MOD;
  return res;
}
int main() {
  string expression;
  cin >> expression;
  expression = "(" + expression + ")";
  bool var_expected = true;
  map<char, int> precedence;
  precedence['&'] = 1;
  precedence['|'] = 1;
  precedence['?'] = 1;
  precedence[')'] = 2;
  precedence['('] = 3;
  for (char ch : expression) {
    if (var_expected) {
      if (ch == '(') {
        stkoper[++stkoper_n] = '(';
      } else {
        var_expected = false;
        fill_n(stknum[++stknum_n], EXPR_NUM, 0);
        for (char v = 'A'; v <= 'D'; v++) {
          if (ch == v || ch == '?') {
            int t = 0;
            for (int j = 0; j < CASE_NUM; j++) {
              if ((j >> v - 'A' & 1) == 1) {
                t += 1 << j;
              }
            }
            stknum[stknum_n][t] = 1;
          }
        }
        for (char v = 'a'; v <= 'd'; v++) {
          if (ch == v || ch == '?') {
            int t = 0;
            for (int j = 0; j < CASE_NUM; j++) {
              if ((j >> v - 'a' & 1) == 0) {
                t += 1 << j;
              }
            }
            stknum[stknum_n][t] = 1;
          }
        }
      }
    } else {
      while (precedence[stkoper[stkoper_n]] <= precedence[ch]) {
        long long num1[EXPR_NUM], num2[EXPR_NUM], result[EXPR_NUM];
        copy_n(stknum[stknum_n--], EXPR_NUM, num2);
        copy_n(stknum[stknum_n--], EXPR_NUM, num1);
        fill_n(result, EXPR_NUM, 0);
        if (stkoper[stkoper_n] == '&' || stkoper[stkoper_n] == '?') {
          and_conv(num1, num2, result);
        }
        if (stkoper[stkoper_n] == '|' || stkoper[stkoper_n] == '?') {
          or_conv(num1, num2, result);
        }
        copy_n(result, EXPR_NUM, stknum[++stknum_n]);
        stkoper_n--;
      }
      if (ch == ')') {
        stkoper_n--;
        var_expected = false;
      } else {
        stkoper[++stkoper_n] = ch;
        var_expected = true;
      }
    }
  }
  int req[CASE_NUM];
  fill_n(req, CASE_NUM, -1);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a, b, c, d, e;
    cin >> a >> b >> c >> d >> e;
    req[(a << 0) + (b << 1) + (c << 2) + (d << 3)] = e;
  }
  long long ans = 0;
  for (int i = 0; i < EXPR_NUM; i++) {
    bool ok = true;
    for (int j = 0; j < CASE_NUM; j++) {
      if (req[j] != -1 && (i >> j & 1) != req[j]) ok = false;
    }
    if (ok) (ans += stknum[1][i]) %= MOD;
  }
  cout << ans << endl;
}
