#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool getmin(T *a, const T &b) {
  if (b < *a) {
    *a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool getmax(T *a, const T &b) {
  if (b > *a) {
    *a = b;
    return true;
  }
  return false;
}
template <class T>
inline void read(T *a) {
  char c;
  while (isspace(c = getchar())) {
  }
  bool flag = 0;
  if (c == '-')
    flag = 1, *a = 0;
  else
    *a = c - 48;
  while (isdigit(c = getchar())) *a = *a * 10 + c - 48;
  if (flag) *a = -*a;
}
const int mo = 1000000007;
template <class T>
T pow(T a, T b, int c = mo) {
  T res = 1;
  for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c)
    if (b & i) res = 1LL * res * a % c;
  return res;
}
const int N = 100000;
const int M = 100;
int n, m;
int s[M][4], ed;
int f[500][N], t1[N], t2[N], t3[N];
char c[N];
void AND(int *a, int *b, int *c, int l, int r) {
  if (l == r) {
    c[l] = 1LL * a[l] * b[l] % mo;
    return;
  }
  int m = (l + r) >> 1, s = m - l + 1;
  for (int i = (l); i <= (m); ++i)
    a[i] = (a[i] + a[i + s]) % mo, b[i] = (b[i] + b[i + s]) % mo;
  AND(a, b, c, l, m);
  AND(a, b, c, m + 1, r);
  for (int i = (l); i <= (m); ++i) c[i] = (c[i] + mo - c[i + s]) % mo;
}
void OR(int *a, int *b, int *c, int l, int r) {
  if (l == r) {
    c[l] = 1LL * a[l] * b[l] % mo;
    return;
  }
  int m = (l + r) >> 1, s = m - l + 1;
  for (int i = (m + 1); i <= (r); ++i)
    a[i] = (a[i] + a[i - s]) % mo, b[i] = (b[i] + b[i - s]) % mo;
  OR(a, b, c, l, m);
  OR(a, b, c, m + 1, r);
  for (int i = (m + 1); i <= (r); ++i) c[i] = (c[i] + mo - c[i - s]) % mo;
}
int solve(int l, int r) {
  int tmp = ++m;
  if (l == r) {
    if (c[l] != '?' && c[l] <= 'D') {
      int p = c[l] - 'A';
      int st = 0;
      for (int i = (0); i <= (n - 1); ++i)
        if (s[i][p]) st |= (1 << i);
      f[m][st] = 1;
    } else if (c[l] != '?') {
      int p = c[l] - 'a';
      int st = 0;
      for (int i = (0); i <= (n - 1); ++i)
        if (!s[i][p]) st |= (1 << i);
      f[m][st] = 1;
    } else {
      for (int p = (0); p <= (3); ++p) {
        int st = 0;
        for (int i = (0); i <= (n - 1); ++i)
          if (s[i][p]) st |= (1 << i);
        f[m][st]++;
      }
      for (int p = (0); p <= (3); ++p) {
        int st = 0;
        for (int i = (0); i <= (n - 1); ++i)
          if (!s[i][p]) st |= (1 << i);
        f[m][st]++;
      }
    }
    return m;
  }
  int num = 0, p = 0;
  for (int i = (l); i <= (r); ++i) {
    if (c[i] == '(')
      ++num;
    else if (c[i] == ')')
      --num;
    else if (!num) {
      p = i;
      break;
    }
  }
  int x = solve(l + 1, p - 2), y = solve(p + 2, r - 1);
  if (c[p] == '|')
    OR(f[x], f[y], f[tmp], 0, (1 << n) - 1);
  else if (c[p] == '&')
    AND(f[x], f[y], f[tmp], 0, (1 << n) - 1);
  else {
    for (int i = (0); i <= ((1 << n) - 1); ++i)
      t1[i] = f[x][i], t2[i] = f[y][i], t3[i] = 0;
    AND(t1, t2, t3, 0, (1 << n) - 1);
    OR(f[x], f[y], f[tmp], 0, (1 << n) - 1);
    for (int i = (0); i <= ((1 << n) - 1); ++i)
      f[tmp][i] = (f[tmp][i] + t3[i]) % mo;
  }
  return tmp;
}
int main() {
  scanf("%s", c);
  int len = strlen(c);
  cin >> n;
  for (int i = (0); i <= (n - 1); ++i) {
    for (int j = (0); j <= (3); ++j) cin >> s[i][j];
    int x;
    cin >> x;
    if (x) ed |= (1 << i);
  }
  int p = solve(0, len - 1);
  cout << f[p][ed] << endl;
  return 0;
}
