#include <bits/stdc++.h>
using namespace std;
const int N = 505, S = 1 << 16, mod = 1e9 + 7;
char s[N];
int len, n, U, f[N][S], tot, x, tar[5];
inline void inc(int& x, const int& y) {
  if ((x += y) >= mod) x -= mod;
}
inline void dec(int& x, const int& y) {
  if ((x -= y) < 0) x += mod;
}
namespace Poly {
inline void FWT_OR(int* f, const int& opt) {
  for (register int i = 1, j, k; i <= U; i <<= 1)
    for (j = 0; j <= U; j += (i << 1))
      for (k = 0; k < i; ++k)
        if (~opt)
          inc(f[i + j + k], f[j + k]);
        else
          dec(f[i + j + k], f[j + k]);
}
inline void FWT_AND(int* f, const int& opt) {
  for (register int i = 1, j, k; i <= U; i <<= 1)
    for (j = 0; j <= U; j += (i << 1))
      for (k = 0; k < i; ++k)
        if (~opt)
          inc(f[j + k], f[i + j + k]);
        else
          dec(f[j + k], f[i + j + k]);
}
};  // namespace Poly
inline int DP(const int& l, const int& r) {
  int now = ++tot;
  register int i;
  if (r - l == 2) {
    if (s[l + 1] >= 'A' && s[l + 1] <= 'D')
      ++f[now][tar[s[l + 1] - 'A']];
    else if (s[l + 1] >= 'a' && s[l + 1] <= 'd')
      ++f[now][U ^ tar[s[l + 1] - 'a']];
    else
      for (i = 0; i < 4; ++i) ++f[now][tar[i]], ++f[now][U ^ tar[i]];
    return now;
  }
  int pos, cur = 0;
  for (i = l + 1; i <= r - 1; ++i)
    if (s[i] == '(')
      ++cur;
    else if (s[i] == ')')
      --cur;
    else if (!cur) {
      pos = i;
      break;
    }
  int ls = DP(l + 1, pos - 1), rs = DP(pos + 1, r - 1);
  if (s[pos] != '&') {
    Poly::FWT_OR(f[ls], 1);
    Poly::FWT_OR(f[rs], 1);
    Poly::FWT_OR(f[now], 1);
    for (i = 0; i <= U; ++i) inc(f[now][i], 1LL * f[ls][i] * f[rs][i] % mod);
    Poly::FWT_OR(f[ls], -1);
    Poly::FWT_OR(f[rs], -1);
    Poly::FWT_OR(f[now], -1);
  }
  if (s[pos] != '|') {
    Poly::FWT_AND(f[ls], 1);
    Poly::FWT_AND(f[rs], 1);
    Poly::FWT_AND(f[now], 1);
    for (i = 0; i <= U; ++i) inc(f[now][i], 1LL * f[ls][i] * f[rs][i] % mod);
    Poly::FWT_AND(f[ls], -1);
    Poly::FWT_AND(f[rs], -1);
    Poly::FWT_AND(f[now], -1);
  }
  return now;
}
int main() {
  register int i, j;
  scanf("%s", s + 1);
  len = strlen(s + 1);
  s[0] = '(';
  s[++len] = ')';
  for (scanf("%d", &n), i = 0; i < n; ++i)
    for (j = 0; j < 5; ++j) scanf("%d", &x), tar[j] |= x << i;
  return U = (1 << n) - 1, printf("%d", f[DP(0, len)][tar[4]]), 0;
}
