#include <bits/stdc++.h>
using namespace std;
int power(int a, int b, int c, int ans = 1) {
  for (; b; b >>= 1, a = (long long)a * a % c)
    if (b & 1) ans = (long long)ans * a % c;
  return ans;
}
char s[555];
int val[333][1 << 16], tot;
const int mod = 1000000007, N = 1 << 16;
int nxt[555], a[N];
inline void add(int &first, const int &second) {
  first += second;
  if (first >= mod) first -= mod;
}
inline void sub(int &first, const int &second) {
  first -= second;
  if (first < 0) first += mod;
}
void fwt_and(int a[], int b[]) {
  for (int d = 1; d < N; d <<= 1) {
    for (int m = d << 1, i = 0; i < N; i += m) {
      for (int j = 0; j < d; j++) {
        add(a[i | j], a[i | j | d]);
        add(b[i | j], b[i | j | d]);
      }
    }
  }
}
void fwt_or(int a[], int b[]) {
  for (int d = 1; d < N; d <<= 1) {
    for (int m = d << 1, i = 0; i < N; i += m) {
      for (int j = 0; j < d; j++) {
        add(a[i | j | d], a[i | j]);
        add(b[i | j | d], b[i | j]);
      }
    }
  }
}
void fwt_and_inv(int a[]) {
  for (int d = 1; d < N; d <<= 1) {
    for (int m = d << 1, i = 0; i < N; i += m) {
      for (int j = 0; j < d; j++) {
        sub(a[i | j], a[i | j | d]);
      }
    }
  }
}
void fwt_or_inv(int a[]) {
  for (int d = 1; d < N; d <<= 1) {
    for (int m = d << 1, i = 0; i < N; i += m) {
      for (int j = 0; j < d; j++) {
        sub(a[i | j | d], a[i | j]);
      }
    }
  }
}
int solve(int st, int ed) {
  int now = ++tot;
  if (st == ed) {
    if (s[st] == '?')
      for (int i = 0; i < 8; i++) val[now][a[i]] = 1;
    else
      val[now][a[s[st] > 'D' ? s[st] - 'a' + 4 : s[st] - 'A']] = 1;
    return now;
  }
  if (nxt[st] == ed) {
    --tot;
    return solve(st + 1, ed - 1);
  }
  int lft = solve(st, nxt[st]);
  int rgt = solve(nxt[st] + 2, ed);
  char c = s[nxt[st] + 1];
  static int a[N], b[N];
  if (c != '|') {
    memcpy(a, val[lft], sizeof val[lft]);
    memcpy(b, val[rgt], sizeof val[rgt]);
    fwt_and(a, b);
    for (int i = 0; i < N; i++) val[now][i] = (long long)a[i] * b[i] % mod;
    fwt_and_inv(val[now]);
  }
  if (c != '&') {
    memcpy(a, val[lft], sizeof val[lft]);
    memcpy(b, val[rgt], sizeof val[rgt]);
    fwt_or(a, b);
    for (int i = 0; i < N; i++) a[i] = (long long)a[i] * b[i] % mod;
    fwt_or_inv(a);
    for (int i = 0; i < N; i++) add(val[now][i], a[i]);
  }
  return now;
}
int main() {
  scanf("%s", s);
  int n;
  cin >> n;
  int st = 0, msk = 0;
  for (int i = 0; i < n; i++) {
    int u = 0, v;
    for (int j = 0; j < 4; j++) {
      scanf("%d", &v);
      u = u + (v << j);
    }
    scanf("%d", &v);
    st |= v << u;
    msk |= 1 << u;
  }
  stack<int> ss;
  for (int i = 0, n = strlen(s); i < n; i++) {
    if (s[i] == '(')
      ss.push(i);
    else if (s[i] == ')')
      nxt[ss.top()] = i, ss.pop();
    else
      nxt[i] = i;
  }
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 16; j++) {
      if (i < 4 && ((j >> i) & 1) || i >= 4 && (~(j >> i - 4) & 1))
        a[i] |= 1 << j;
    }
  }
  int now = solve(0, strlen(s) - 1);
  long long ans = 0;
  for (int i = 0; i < N; i++)
    if ((i & msk) == st) ans += val[now][i];
  cout << ans % mod << endl;
  return 0;
}
