#include <bits/stdc++.h>
using namespace std;
const int V = 4, S = 16, L = 65536, F = 65535, M = 1000000007;
int var[256], opt[256];
char s[505];
int n, m, root, pos, s0, s1, ans;
int f[233][L];
inline void inc(int &x, const int &y) {
  x += y;
  if (x > M) x -= M;
}
inline void dec(int &x, const int &y) {
  x -= y;
  if (x < 0) x += M;
}
void add(int *x, const int *y) {
  for (int i = 0; i < L; i++) inc(x[i], y[i]);
}
int getone(const int &v) {
  int r = 0;
  for (int i = 0; i < S; i++)
    if (i >> v & 1) r |= 1 << i;
  return r;
}
void rev(int *a) {
  for (int i = 0; i < L >> 1; i++) swap(a[i], a[F - i]);
}
int tl[L], tr[L], tx[2][L];
void fwt(const int *a, int *b, int flag) {
  memcpy(b, a, L << 2);
  for (int i = 0; i < S; i++)
    for (int j = 0; j < L; j++)
      if (!(j >> i & 1)) {
        if (flag == 1)
          inc(b[j ^ (1 << i)], b[j]);
        else
          dec(b[j ^ (1 << i)], b[j]);
      }
}
void mult(int *a, int *b, int *c) {
  fwt(a, tl, 1);
  fwt(b, tr, 1);
  for (int i = 0; i < L; i++) tl[i] = (long long)tl[i] * tr[i] % M;
  fwt(tl, c, -1);
}
int work(int &pos) {
  if (s[pos] != '(') {
    int v = var[s[pos++]];
    if (v == -1)
      for (v = 0; v < V; v++) f[n][getone(v)] = f[n][F ^ getone(v)] = 1;
    else if (v < V)
      f[n][getone(v)] = 1;
    else
      f[n][F ^ getone(v - V)] = 1;
    return n++;
  }
  int l, r, x = n++, p;
  l = work(++pos);
  pos++;
  p = opt[s[pos++]];
  r = work(++pos);
  pos++;
  if (p == 1 || p == -1) mult(f[l], f[r], tx[1]);
  if (p == 0 || p == -1) {
    rev(f[l]), rev(f[r]);
    mult(f[l], f[r], tx[0]);
    rev(tx[0]);
    rev(f[l]), rev(f[r]);
  }
  if (p == -1)
    add(f[x], tx[0]), add(f[x], tx[1]);
  else
    add(f[x], tx[p]);
  return x;
}
void init() {
  memset(var, -1, sizeof(var));
  memset(opt, -1, sizeof(opt));
  var['A'] = 0, var['B'] = 1, var['C'] = 2, var['D'] = 3;
  var['a'] = 4, var['b'] = 5, var['c'] = 6, var['d'] = 7;
  opt['&'] = 0, opt['|'] = 1;
}
int main() {
  init();
  scanf("%s", s);
  s[strlen(s)] = ')';
  root = work(pos);
  scanf("%d", &m);
  for (int i = 1, a, b, c, d, e, t; i <= m; i++) {
    scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
    t = a + (b << 1) + (c << 2) + (d << 3);
    if (e)
      s1 |= 1 << t;
    else
      s0 |= 1 << t;
  }
  for (int i = 0; i < L; i++)
    if ((i & s1) == s1 && (i & s0) == 0) inc(ans, f[root][i]);
  ans = (ans % M + M) % M;
  printf("%d\n", ans);
  return 0;
}
