#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXLEN = 500;
const int MOD = 1000000007;
const int MAXX = 1 + 2 * 83;
typedef struct X {
  int l, r;
  char op;
} X;
char s[MAXLEN + 1];
char *at;
X x[MAXX];
int nx;
int nq;
int ways[MAXX][1 << 16];
inline void inc(int &a, const int &b) {
  if ((a += b) >= MOD) a -= MOD;
}
inline void sub(int &a, const int &b) {
  if ((a -= b) < 0) a += MOD;
}
inline int mult(const int &a, const int &b) { return (long long)a * b % MOD; }
void print16(int x) {
  for (int i = (0); i < (16); ++i) printf("%c", ((x >> i) & 1) ? '1' : '0');
}
int parse() {
  char c = *at++;
  if (isalpha(c) || c == '?') {
    for (int low = (0); low < (2); ++low)
      for (char cc = (low ? 'a' : 'A'); cc <= (low ? 'd' : 'D'); ++cc)
        if (c == cc || c == '?')
          for (int i = (0); i < (1 << 16); ++i) {
            bool ok = true;
            for (int j = (0); j < (16); ++j)
              if (((i >> j) & 1) !=
                  (((j >> (tolower(cc) - 'a')) & 1) == (low ? 0 : 1))) {
                ok = false;
                break;
              }
            if (ok) ++ways[nx][i];
          }
    x[nx].l = x[nx].r = -1, x[nx].op = 'x';
    return nx++;
  } else {
    assert(c == '(');
    int l = parse();
    c = *at++;
    assert(c == ')');
    c = *at++;
    assert(c == '|' || c == '&' || c == '?');
    char op = c;
    c = *at++;
    assert(c == '(');
    int r = parse();
    c = *at++;
    assert(c == ')');
    x[nx].l = l, x[nx].op = op, x[nx].r = r;
    return nx++;
  }
}
int f[1 << 16], fl[1 << 16], fr[1 << 16];
void calc(int at) {
  if (x[at].op == 'x') return;
  calc(x[at].l);
  calc(x[at].r);
  if (x[at].op == '|' || x[at].op == '?') {
    for (int i = (0); i < (1 << 16); ++i) fl[i] = ways[x[at].l][i];
    for (int j = (0); j < (16); ++j)
      for (int i = (0); i < (1 << 16); ++i)
        if ((i >> j) & 1) inc(fl[i], fl[i ^ (1 << j)]);
    for (int i = (0); i < (1 << 16); ++i) fr[i] = ways[x[at].r][i];
    for (int j = (0); j < (16); ++j)
      for (int i = (0); i < (1 << 16); ++i)
        if ((i >> j) & 1) inc(fr[i], fr[i ^ (1 << j)]);
    for (int i = (0); i < (1 << 16); ++i) f[i] = mult(fl[i], fr[i]);
    for (int j = (0); j < (16); ++j)
      for (int i = (0); i < (1 << 16); ++i)
        if ((i >> j) & 1) sub(f[i], f[i ^ (1 << j)]);
    for (int i = (0); i < (1 << 16); ++i) inc(ways[at][i], f[i]);
  }
  if (x[at].op == '&' || x[at].op == '?') {
    for (int i = (0); i < (1 << 16); ++i) fl[i] = ways[x[at].l][i];
    for (int j = (0); j < (16); ++j)
      for (int i = (0); i < (1 << 16); ++i)
        if ((i >> j) & 1) inc(fl[i ^ (1 << j)], fl[i]);
    for (int i = (0); i < (1 << 16); ++i) fr[i] = ways[x[at].r][i];
    for (int j = (0); j < (16); ++j)
      for (int i = (0); i < (1 << 16); ++i)
        if ((i >> j) & 1) inc(fr[i ^ (1 << j)], fr[i]);
    for (int i = (0); i < (1 << 16); ++i) f[i] = mult(fl[i], fr[i]);
    for (int j = (0); j < (16); ++j)
      for (int i = (0); i < (1 << 16); ++i)
        if ((i >> j) & 1) sub(f[i ^ (1 << j)], f[i]);
    for (int i = (0); i < (1 << 16); ++i) inc(ways[at][i], f[i]);
  }
}
void run() {
  scanf("%s%d", s, &nq);
  int zero = 0, one = 0;
  for (int i = (0); i < (nq); ++i) {
    int a, b, c, d, e;
    scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
    int id = a + 2 * b + 4 * c + 8 * d;
    if (e == 0)
      zero |= 1 << id;
    else
      one |= 1 << id;
  }
  nx = 0;
  memset(ways, 0, sizeof(ways));
  at = s;
  int root = parse();
  calc(root);
  assert(*at == '\0');
  int ret = 0;
  for (int i = (0); i < (1 << 16); ++i)
    if ((i & one) == one && (i & zero) == 0) inc(ret, ways[root][i]);
  printf("%d\n", ret);
}
int main() {
  run();
  return 0;
}
