#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int Mod = 1e9 + 7;
const int maxn = 500;
struct data {
  int cnt[65536];
  data() { memset(cnt, 0, sizeof cnt); }
  friend data operator+(const data &first, const data &second) {
    data ans;
    for (int i = (0), _end_ = (65536); i < _end_; ++i)
      ans.cnt[i] = (first.cnt[i] + second.cnt[i]) % Mod;
    return ans;
  }
};
data I[256];
int n;
char s[maxn + 5];
data combine(const data &first, const data &second, const char &ty) {
  data ans;
  vector<int> A, B;
  for (int i = (0), _end_ = (65536); i < _end_; ++i)
    if (first.cnt[i]) A.push_back(i);
  for (int i = (0), _end_ = (65536); i < _end_; ++i)
    if (second.cnt[i]) B.push_back(i);
  if ((long long)(int((A).size())) * (int((B).size())) <= 16 * 65536) {
    for (auto u : A)
      for (auto v : B)
        (ans.cnt[(ty == '|' ? (u | v) : (u & v))] +=
         (long long)first.cnt[u] * second.cnt[v] % Mod) %= Mod;
  }
  static int a[65537], b[65537], c[65537];
  for (int i = (0), _end_ = (65536); i < _end_; ++i) a[i] = first.cnt[i];
  for (int i = (0), _end_ = (65536); i < _end_; ++i) b[i] = second.cnt[i];
  for (int i = (0), _end_ = (16); i < _end_; ++i)
    for (int j = (0), _end_ = (65536); j < _end_; ++j) {
      if ((j >> i & 1) ^ (ty == '|')) {
        (a[j ^ (1 << i)] += a[j]) %= Mod;
        (b[j ^ (1 << i)] += b[j]) %= Mod;
      }
    }
  for (int i = (0), _end_ = (65536); i < _end_; ++i)
    c[i] = (long long)a[i] * b[i] % Mod;
  for (int i = (0), _end_ = (16); i < _end_; ++i)
    for (int j = (0), _end_ = (65536); j < _end_; ++j) {
      if ((j >> i & 1) ^ (ty == '|')) {
        (c[j ^ (1 << i)] -= c[j]) %= Mod;
      }
    }
  for (int i = (0), _end_ = (65536); i < _end_; ++i) ans.cnt[i] = c[i];
  return ans;
}
inline data work(const int &l, const int &r) {
  int now = 1;
  data ret;
  bool flag = 0;
  for (int i = l; i < r; ++i) {
    if (!now) {
      if (s[i] != '?' && s[i] != '&' && s[i] != '|') return data();
      flag = 1;
    } else {
      data lyc;
      int st = i;
      if (s[i] == '(') {
        if (!now) return data();
        int tmp = 0;
        while (i < r) {
          if (s[i] == '(')
            ++tmp;
          else if (s[i] == ')')
            --tmp;
          if (!tmp) break;
          ++i;
        }
        lyc = work(st + 1, i);
      } else
        lyc = I[(int)s[i]];
      if (!flag)
        ret = lyc;
      else {
        if (s[st - 1] != '?')
          ret = combine(ret, lyc, s[st - 1]);
        else
          ret = combine(ret, lyc, '|') + combine(ret, lyc, '&');
      }
    }
    now = !now;
  }
  if (now) return data();
  return ret;
}
int main() {
  scanf("%s", s);
  n = strlen(s);
  static int val[256] = {0};
  for (int i = 15; i >= 0; --i) {
    for (int j = (0), _end_ = (256); j < _end_; ++j) val[j] <<= 1;
    val['A'] |= (i & 1);
    val['a'] |= !(i & 1);
    val['B'] |= (i >> 1 & 1);
    val['b'] |= !(i >> 1 & 1);
    val['C'] |= (i >> 2 & 1);
    val['c'] |= !(i >> 2 & 1);
    val['D'] |= (i >> 3 & 1);
    val['d'] |= !(i >> 3 & 1);
  }
  I['A'].cnt[val['A']] = 1;
  I['a'].cnt[val['a']] = 1;
  I['B'].cnt[val['B']] = 1;
  I['b'].cnt[val['b']] = 1;
  I['C'].cnt[val['C']] = 1;
  I['c'].cnt[val['c']] = 1;
  I['D'].cnt[val['D']] = 1;
  I['d'].cnt[val['d']] = 1;
  I['?'] =
      I['A'] + I['a'] + I['B'] + I['b'] + I['C'] + I['c'] + I['D'] + I['d'];
  data tmp = work(0, n);
  int m;
  vector<pair<int, int> > all;
  scanf("%d", &m);
  for (int i = (0), _end_ = (m); i < _end_; ++i) {
    int a, b, c, d, e;
    scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
    all.push_back(make_pair(a | (b << 1) | (c << 2) | (d << 3), e));
  }
  int ret = 0;
  for (int i = (0), _end_ = (65536); i < _end_; ++i) {
    bool flag = 1;
    for (auto it : all)
      if ((i >> it.first & 1) != it.second) {
        flag = 0;
        break;
      }
    if (flag) (ret += tmp.cnt[i]) %= Mod;
  }
  (ret += Mod) %= Mod;
  printf("%d\n", ret);
  return 0;
}
