#include <bits/stdc++.h>
using namespace std;
int dp[500][(1 << (1 << 4))], tdp[2][(1 << (1 << 4))];
string s;
int curptr = 0;
int getMask(int j, int req) {
  int mask = 0;
  for (int i = 0; i < (1 << 4); i++) {
    if (((i >> j) & 1) == req) mask |= (1 << i);
  }
  return mask;
}
void rev(int *p) { reverse(p, p + (1 << (1 << 4))); }
void add(int &a, int b) {
  a += b;
  if (a >= 1000000007) a -= 1000000007;
}
void sub(int &a, int b) {
  a -= b;
  if (a < 0) a += 1000000007;
}
void mul(int &a, int b) {
  long long x = a;
  x *= b;
  a = (x % 1000000007);
}
int s1[(1 << (1 << 4))], s2[(1 << (1 << 4))];
void eAndD(int *t1, int *t2, int *ans) {
  for (int i = 0; i < (1 << (1 << 4)); i++) {
    s1[i] = t1[i];
    s2[i] = t2[i];
    ans[i] = 1;
  }
  for (int j = 0; j < (1 << 4); j++) {
    for (int i = 0; i < (1 << (1 << 4)); i++)
      if ((i >> j) & 1) {
        add(s1[i], s1[i - (1 << j)]);
        add(s2[i], s2[i - (1 << j)]);
      }
  }
  for (int i = 0; i < (1 << (1 << 4)); i++) {
    mul(ans[i], s1[i]);
    mul(ans[i], s2[i]);
  }
  for (int j = 0; j < (1 << 4); j++) {
    for (int i = 0; i < (1 << (1 << 4)); i++)
      if ((i >> j) & 1) {
        sub(ans[i], ans[i - (1 << j)]);
      }
  }
}
int f(int &ptr) {
  if (s[ptr] != '(') {
    char v = s[ptr++];
    int cur = curptr++;
    if (v == '?') {
      for (int i = 0; i < 4; i++) {
        dp[cur][getMask(i, 1)] = 1;
        dp[cur][getMask(i, 0)] = 1;
      }
    } else if (v >= 'A' && v <= 'D') {
      dp[cur][getMask(v - 'A', 1)] = 1;
    } else {
      dp[cur][getMask(v - 'a', 0)] = 1;
    }
    return cur;
  }
  int cur = curptr++;
  int l = f(++ptr);
  ptr++;
  char op = s[ptr++];
  int r = f(++ptr);
  ptr++;
  eAndD(dp[l], dp[r], tdp[0]);
  rev(dp[l]);
  rev(dp[r]);
  eAndD(dp[l], dp[r], tdp[1]);
  rev(tdp[1]);
  rev(dp[l]);
  rev(dp[r]);
  if (op == '?' || op == '|')
    for (int i = 0; i < (1 << (1 << 4)); i++) add(dp[cur][i], tdp[0][i]);
  if (op == '?' || op == '&')
    for (int i = 0; i < (1 << (1 << 4)); i++) add(dp[cur][i], tdp[1][i]);
  return cur;
}
int main() {
  cin >> s;
  int ptr = 0;
  int pp = f(ptr);
  int n;
  cin >> n;
  int req[(1 << 4)];
  for (int i = 0; i < (1 << 4); i++) req[i] = -1;
  for (int i = 0; i < n; i++) {
    int mask = 0, x;
    for (int j = 0; j < 4; j++) {
      cin >> x;
      if (x) mask |= (1 << j);
    }
    cin >> req[mask];
  }
  int ans = 0;
  for (int i = 0; i < (1 << (1 << 4)); i++) {
    bool ok = true;
    for (int j = 0; j < (1 << 4); j++)
      if (req[j] != -1 && (((i >> j) & 1) != req[j])) ok = false;
    if (ok) add(ans, dp[pp][i]);
  }
  cout << ans << endl;
}
