#include <bits/stdc++.h>
using namespace std;
const int N = 505, M = 16, P = 1000000007;
using ll = long long;
char s[N], c[N];
int n, m, kk, fa[N], val[M][5];
vector<int> v[N];
void FWT_or(ll a[], ll fl = 1) {
  for (int s = 2, l = 1; s <= m; s <<= 1, l <<= 1)
    for (int i = 0; i < m; i += s)
      for (int j = 0; j < l; j++)
        a[i + j + l] = (a[i + j + l] + fl * a[i + j]) % P;
}
void FWT_and(ll a[], ll fl = 1) {
  for (int s = 2, l = 1; s <= m; s <<= 1, l <<= 1)
    for (int i = 0; i < m; i += s)
      for (int j = 0; j < l; j++) a[i + j] = (a[i + j] + fl * a[i + j + l]) % P;
}
int dp[N][1 << M];
ll a[1 << M], b[1 << M];
inline void st(int aa[], int bb[]) {
  for (int i = 0; i < m; i++) a[i] = aa[i], b[i] = bb[i];
}
void get() {
  for (int i = 0; i < m; i++) a[i] = a[i] * b[i] % P;
}
void out(int f[]) {
  for (int i = 0; i < m; i++) f[i] = (f[i] + a[i]) % P;
}
void dfs(int pos) {
  if (v[pos].empty()) {
    if (c[pos] == '?') {
      for (int i = 0; i < 4; i++) {
        int msk = 0;
        for (int j = 0; j < n; j++) msk |= val[j][i] << j;
        dp[pos][msk] = (dp[pos][msk] + 1) % P;
        dp[pos][(m - 1) ^ msk] = (dp[pos][(m - 1) ^ msk] + 1) % P;
      }
    } else {
      int i = c[pos] - (isupper(c[pos]) ? 'A' : 'a'), msk = 0;
      for (int j = 0; j < n; j++) msk |= val[j][i] << j;
      int &r = dp[pos][isupper(c[pos]) ? msk : ((m - 1) ^ msk)];
      r = (r + 1) % P;
    }
    return;
  }
  dfs(v[pos][0]);
  dfs(v[pos][1]);
  if (c[pos] != '&') {
    st(dp[v[pos][0]], dp[v[pos][1]]);
    FWT_or(a);
    FWT_or(b);
    get();
    FWT_or(a, P - 1);
    out(dp[pos]);
  }
  if (c[pos] != '|') {
    st(dp[v[pos][0]], dp[v[pos][1]]);
    FWT_and(a);
    FWT_and(b);
    get();
    FWT_and(a, P - 1);
    out(dp[pos]);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> (s + 1) >> n;
  kk = strlen(s + 1);
  m = 1 << n;
  int idx = 1, pos = 1;
  for (int i = 1; i <= kk; i++)
    if (s[i] == '(')
      v[pos].push_back(++idx), fa[idx] = pos, pos = idx;
    else if (s[i] == ')')
      pos = fa[pos];
    else
      c[pos] = s[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 5; j++) cin >> val[i][j];
  dfs(1);
  int msk = 0;
  for (int i = 0; i < n; i++) msk |= val[i][4] << i;
  cout << dp[1][msk] << endl;
  return 0;
}
