#include <bits/stdc++.h>
using namespace std;
const int MOD = int(1e9) + 7;
inline int add(int a, int b) {
  return (a + b >= MOD) ? (a + b - MOD) : (a + b);
}
inline void inc(int &a, int b) { a = add(a, b); }
inline int sub(int a, int b) { return (a - b < 0) ? (a - b + MOD) : (a - b); }
inline void dec(int &a, int b) { a = sub(a, b); }
inline int mul(int a, int b) { return (a * 1ll * b) % MOD; }
const int N = 200 + 5;
const int V = 4;
const int S = (1 << V);
const int M = (1 << S);
char s[N * 6];
int n, d[N][M];
const string vars = "ABCDabcd";
const string ops = "&|";
int maskone(int v) {
  int mask = 0;
  for (int i = 0; i < S; ++i)
    if ((i >> v) & 1) mask |= (1 << i);
  return mask;
}
void ____negate(int *a) { reverse(a, a + M); }
void copy(const int *a, int *b) {
  for (int i = 0; i < M; ++i) b[i] = a[i];
}
void add(const int *a, int *b) {
  for (int i = 0; i < M; ++i) inc(b[i], a[i]);
}
void sumsub(const int *a, int *b, int d) {
  copy(a, b);
  for (int i = 0; i < S; ++i)
    for (int mask = 0; mask < M; ++mask)
      if (!(mask & (1 << i))) {
        if (d == +1)
          inc(b[mask ^ (1 << i)], b[mask]);
        else if (d == -1)
          dec(b[mask ^ (1 << i)], b[mask]);
        else
          throw;
      }
}
int t1[M], t2[M], tops[2][M];
void door(const int *l, const int *r, int *v) {
  sumsub(l, t1, +1);
  sumsub(r, t2, +1);
  for (int i = 0; i < M; ++i) t1[i] = mul(t1[i], t2[i]);
  sumsub(t1, v, -1);
}
int parse(int &pos) {
  if (s[pos] != '(') {
    int v = vars.find(s[pos++]);
    if (v == -1) {
      for (int i = 0; i < V; ++i) {
        d[n][maskone(i)] = 1;
        d[n][(M - 1) ^ maskone(i)] = 1;
      }
    } else {
      if (v < V)
        d[n][maskone(v)] = 1;
      else
        d[n][(M - 1) ^ maskone(v - V)] = 1;
    }
    return n++;
  }
  int me = n++;
  int l = parse(++pos);
  assert(s[pos++] == ')');
  int v = ops.find(s[pos++]);
  int r = parse(++pos);
  assert(s[pos++] == ')');
  door(d[l], d[r], tops[1]);
  ____negate(d[l]);
  ____negate(d[r]);
  door(d[l], d[r], tops[0]);
  ____negate(tops[0]);
  ____negate(d[r]);
  ____negate(d[l]);
  if (v == -1)
    for (int i = 0; i < ops.size(); ++i) add(tops[i], d[me]);
  else
    add(tops[v], d[me]);
  return me;
}
int req[1 << V];
int main() {
  scanf("%s", s);
  int pos = 0;
  int root = parse(pos);
  scanf("%d", &n);
  for (int i = 0; i < S; ++i) req[i] = -1;
  for (int i = 0; i < n; ++i) {
    int mask = 0;
    for (int j = 0; j < V; ++j) {
      int a;
      scanf("%d", &a);
      mask |= (a << j);
    }
    scanf("%d", &req[mask]);
  }
  int res = 0;
  for (int i = 0; i < M; ++i) {
    bool good = true;
    for (int j = 0; j < S; ++j)
      good &= (req[j] == -1 || req[j] == ((i >> j) & 1));
    if (good) inc(res, d[root][i]);
  }
  printf("%d\n", res);
  return 0;
}
