#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:640000000")
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int mod = (int)1e9 + 7;
string s;
int len;
int ptr = 0;
void add(int &x, int y) {
  x += y;
  if (x >= mod) x -= mod;
}
void conv(vector<int> &v) {
  for (int i = 0; i < 16; i++) {
    for (int mask = 0; mask < (1 << 16); mask++)
      if (!(mask & (1 << i))) {
        add(v[mask ^ (1 << i)], v[mask]);
      }
  }
}
void inv(vector<int> &v) {
  for (int i = 0; i < 16; i++) {
    for (int mask = 0; mask < (1 << 16); mask++)
      if (!(mask & (1 << i))) {
        add(v[mask ^ (1 << i)], mod - v[mask]);
      }
  }
}
void oor(vector<int> L, vector<int> R, vector<int> &cur) {
  conv(L);
  conv(R);
  for (int i = 0; i < (1 << 16); i++) {
    cur[i] = L[i] * 1LL * R[i] % mod;
  }
  inv(cur);
}
void add(vector<int> &res, vector<int> &cur) {
  for (int i = 0; i < (1 << 16); i++) {
    add(res[i], cur[i]);
  }
}
vector<int> go() {
  vector<int> L, R, res(1 << 16, 0);
  if (s[ptr] == '(') {
    ptr++;
    L = go();
    ptr++;
    char ch = s[ptr];
    ptr++;
    ptr++;
    R = go();
    ptr++;
    vector<int> cur(1 << 16);
    if (ch != '|') {
      reverse((L).begin(), (L).end());
      reverse((R).begin(), (R).end());
      oor(L, R, cur);
      reverse((cur).begin(), (cur).end());
      add(res, cur);
      reverse((L).begin(), (L).end());
      reverse((R).begin(), (R).end());
    }
    if (ch != '&') {
      oor(L, R, cur);
      add(res, cur);
    }
  } else if (s[ptr] == '?') {
    for (int i = 0; i < 4; i++) {
      int cura = 0;
      int curb = 0;
      for (int mask = 0; mask < (1 << 4); mask++) {
        cura = cura * 2 + ((mask >> i) & 1);
        curb = curb * 2 + 1 - ((mask >> i) & 1);
      }
      res[cura]++;
      res[curb]++;
    }
    ptr++;
  } else if (s[ptr] >= 'a' && s[ptr] <= 'z') {
    int cur = 0;
    for (int mask = 0; mask < (1 << 4); mask++) {
      cur = cur * 2 + 1 - ((mask >> (s[ptr] - 'a')) & 1);
    }
    res[cur]++;
    ptr++;
  } else {
    int cur = 0;
    for (int mask = 0; mask < (1 << 4); mask++) {
      cur = cur * 2 + ((mask >> (s[ptr] - 'A')) & 1);
    }
    res[cur]++;
    ptr++;
  }
  return res;
}
int main() {
  cin >> s;
  len = (int)(s).size();
  vector<int> res = go();
  int n;
  cin >> n;
  int exact = 0;
  int mask = 0;
  for (int i = 0; i < n; i++) {
    int pos = 0;
    for (int j = 0; j < 4; j++) {
      int x;
      cin >> x;
      pos += (1 << j) * x;
    }
    int val;
    cin >> val;
    exact ^= (1 << ((1 << 4) - 1 - pos));
    mask ^= (1 << ((1 << 4) - 1 - pos)) * val;
  }
  int ans = 0;
  for (int i = 0; i < (1 << 16); i++) {
    bool ok = true;
    for (int j = 0; j < (1 << 4); j++)
      if (exact & (1 << j)) {
        ok &= (mask & (1 << j)) == (i & (1 << j));
      }
    if (ok) add(ans, res[i]);
  }
  cout << ans << endl;
  return 0;
}
