#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, inv_2 = (mod + 1) / 2;
void Inc(int &x, int y) { x += y, x -= x >= mod ? mod : 0; }
void Dec(int &x, int y) { x -= y, x += x < 0 ? mod : 0; }
int mul(int x, int y) { return 1LL * x * y % mod; }
const int N = 1 << 16;
void fwt_and(vector<int> &f, int type) {
  for (int mid = 1; mid < N; mid <<= 1)
    for (int j = 0; j < N; j += (mid << 1))
      for (int k = 0; k < mid; k++)
        if (type == 1)
          Inc(f[j + k], f[j + k + mid]);
        else
          Dec(f[j + k], f[j + k + mid]);
}
void fwt_or(vector<int> &f, int type) {
  for (int mid = 1; mid < N; mid <<= 1)
    for (int j = 0; j < N; j += (mid << 1))
      for (int k = 0; k < mid; k++)
        if (type == 1)
          Inc(f[j + k + mid], f[j + k]);
        else
          Dec(f[j + k + mid], f[j + k]);
}
vector<int> dp[818], tmp_1, tmp_2;
int node_cnt = 0;
void dfs(int &x, string str) {
  if (!x) x = ++node_cnt;
  vector<int> &now = dp[x];
  now.resize(N, 0);
  if ((int)str.size() == 1) {
    for (int i = 0; i < 4; i++) {
      if (str[0] == '?' || str[0] == 'A' + i) {
        int sta = 0;
        for (int t = 0; t < 16; t++) sta |= ((t >> i) & 1) << t;
        now[sta]++;
      }
    }
    for (int i = 0; i < 4; i++) {
      if (str[0] == '?' || str[0] == 'a' + i) {
        int sta = 0;
        for (int t = 0; t < 16; t++) sta |= (((t >> i) & 1) ^ 1) << t;
        now[sta]++;
      }
    }
    return;
  }
  int ls = 0, rs = 0, pos = -1, len = str.size();
  for (int i = 0, sum = 0; i < len; i++) {
    if (str[i] == '(') sum++;
    if (str[i] == ')') sum--;
    if (sum == 0) {
      pos = i + 1;
      break;
    }
  }
  dfs(ls, str.substr(1, pos - 2));
  dfs(rs, str.substr(pos + 2, len - pos - 3));
  if (str[pos] == '&' || str[pos] == '?') {
    tmp_1 = dp[ls], tmp_2 = dp[rs];
    fwt_and(tmp_1, 1), fwt_and(tmp_2, 1);
    for (int i = 0; i < N; i++) tmp_1[i] = mul(tmp_1[i], tmp_2[i]);
    fwt_and(tmp_1, -1);
    for (int i = 0; i < N; i++) Inc(now[i], tmp_1[i]);
  }
  if (str[pos] == '|' || str[pos] == '?') {
    tmp_1 = dp[ls], tmp_2 = dp[rs];
    fwt_or(tmp_1, 1), fwt_or(tmp_2, 1);
    for (int i = 0; i < N; i++) tmp_1[i] = mul(tmp_1[i], tmp_2[i]);
    fwt_or(tmp_1, -1);
    for (int i = 0; i < N; i++) Inc(now[i], tmp_1[i]);
  }
}
int Ans, T, root;
int Sta[20], Res[20];
string input;
int main() {
  cin >> input >> T;
  dfs(root, input);
  for (int i = 1, A, B, C, D; i <= T; i++) {
    cin >> A >> B >> C >> D >> Res[i];
    Sta[i] = (A << 0) | (B << 1) | (C << 2) | (D << 3);
  }
  for (int S = 0; S < N; S++) {
    int flag = 1;
    for (int i = 1; i <= T && flag; i++)
      if (((S >> Sta[i]) & 1) != Res[i]) flag = 0;
    if (flag) Inc(Ans, dp[root][S]);
  }
  printf("%d\n", Ans);
  return 0;
}
