#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3F3F3F3F;
const int NEGINF = 0xC0C0C0C0;
const double EPS = 1e-8;
const long long MOD = 1e9 + 7;
const int nvar = 4;
const int smask = 1 << nvar;
const int nmask = 1 << smask;
const int maskq[] = {21845, 43690, 13107, 52428, 3855, 61680, 255, 65280};
int cmp(double x, double y = 0, double tol = EPS) {
  return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}
struct node {
  int type;
  char val;
  int ch[2];
};
node nodes[500];
int nn;
int parse(const string &s, int &pos) {
  int r = nn++;
  if (s[pos] != '(') {
    nodes[r].type = 0;
    nodes[r].val = s[pos++];
    return r;
  }
  nodes[r].type = 1;
  pos++;
  nodes[r].ch[0] = parse(s, pos);
  pos++;
  nodes[r].val = s[pos++];
  pos++;
  nodes[r].ch[1] = parse(s, pos);
  pos++;
  return r;
}
long long DP[200][nmask];
void Fand(long long *V, int sz) {
  for (int S = 2; S <= sz; S *= 2) {
    for (int i = 0; i < sz; i += S) {
      for (int j = (0); j < (S / 2); ++j)
        V[i + j] = (V[i + j] + V[i + j + S / 2]) % MOD;
    }
  }
}
void For(long long *V, int sz) {
  for (int S = 2; S <= sz; S *= 2) {
    for (int i = 0; i < sz; i += S) {
      for (int j = (0); j < (S / 2); ++j)
        V[i + j + S / 2] = (V[i + j] + V[i + j + S / 2]) % MOD;
    }
  }
}
void Finvand(long long *V, int sz) {
  for (int S = sz; S >= 2; S /= 2) {
    for (int i = 0; i < sz; i += S) {
      for (int j = (0); j < (S / 2); ++j)
        V[i + j] = ((((V[i + j] - V[i + j + S / 2]) % (MOD)) + (MOD)) % (MOD));
    }
  }
}
void Finvor(long long *V, int sz) {
  for (int S = sz; S >= 2; S /= 2) {
    for (int i = 0; i < sz; i += S) {
      for (int j = (0); j < (S / 2); ++j)
        V[i + j + S / 2] =
            ((((-V[i + j] + V[i + j + S / 2]) % (MOD)) + (MOD)) % (MOD));
    }
  }
}
void do_and(long long inp1[], long long inp2[], long long ans[]) {
  long long tmp1[nmask], tmp2[nmask];
  memcpy(tmp1, inp1, sizeof(tmp1));
  memcpy(tmp2, inp2, sizeof(tmp2));
  Fand(tmp1, nmask);
  Fand(tmp2, nmask);
  for (int i = (0); i < (nmask); ++i) ans[i] = (tmp1[i] * tmp2[i]) % MOD;
  Finvand(ans, nmask);
}
void do_or(long long inp1[], long long inp2[], long long ans[]) {
  long long tmp1[nmask], tmp2[nmask];
  memcpy(tmp1, inp1, sizeof(tmp1));
  memcpy(tmp2, inp2, sizeof(tmp2));
  For(tmp1, nmask);
  For(tmp2, nmask);
  for (int i = (0); i < (nmask); ++i) ans[i] = (tmp1[i] * tmp2[i]) % MOD;
  Finvor(ans, nmask);
}
long long tmp[nmask];
void doit(int n) {
  if (nodes[n].type == 0) {
    if (nodes[n].val == '?') {
      for (int i = (0); i < (8); ++i) DP[n][maskq[i]] = 1;
      return;
    }
    int var, val;
    if (nodes[n].val >= 'a' && nodes[n].val <= 'd') {
      val = 0;
      var = nodes[n].val - 'a';
    } else {
      val = 1;
      var = nodes[n].val - 'A';
    }
    int x = 0;
    for (int i = (0); i < (smask); ++i)
      if (((i >> var) & 1) == val) x |= 1 << i;
    DP[n][x] = 1;
    return;
  }
  doit(nodes[n].ch[0]);
  doit(nodes[n].ch[1]);
  if (nodes[n].val != '|') {
    do_and(DP[nodes[n].ch[0]], DP[nodes[n].ch[1]], tmp);
    for (int i = (0); i < (nmask); ++i) DP[n][i] = (DP[n][i] + tmp[i]) % MOD;
  }
  if (nodes[n].val != '&') {
    do_or(DP[nodes[n].ch[0]], DP[nodes[n].ch[1]], tmp);
    for (int i = (0); i < (nmask); ++i) DP[n][i] = (DP[n][i] + tmp[i]) % MOD;
  }
}
int main() {
  nn = 0;
  string s;
  cin >> s;
  int t = 0;
  parse(s, t);
  doit(0);
  int n;
  cin >> n;
  vector<int> req(smask, -1);
  for (int _ = (0); _ < (n); ++_) {
    int tmp = 0;
    for (int __ = (0); __ < (nvar); ++__) {
      int a;
      cin >> a;
      tmp = tmp | (a << __);
    }
    cin >> req[tmp];
  }
  long long ans = 0;
  for (int mask = (0); mask < (nmask); ++mask) {
    bool good = true;
    for (int i = (0); i < (smask); ++i)
      if (req[i] != -1)
        if (req[i] != ((mask >> i) & 1)) good = false;
    if (good) ans = (ans + DP[0][mask]) % MOD;
  }
  cout << ans << endl;
  return 0;
}
