#include <bits/stdc++.h>
using namespace std;
const int MOD(1000000000 + 7);
const int Max_N(505);
constexpr int Mult(int a, int b) { return a * 1LL * b % MOD; }
constexpr int Add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }
constexpr int Sub(int a, int b) { return a - b < 0 ? a - b + MOD : a - b; }
inline void upd(int &a, int b) { a = Add(a, b); }
inline void inc(int &a, int b) { a = Sub(a, b); }
int N, V, F[Max_N][1 << (1 << 4)], temp[1 << (1 << 4)];
char S[Max_N];
void AND(int F[1 << (1 << 4)], bool rev = false) {
  for (int i = 0; i < (1 << 4); ++i)
    for (int S = 0; S < (1 << (1 << 4)); ++S)
      if (S & (1 << i))
        rev ? inc(F[S - (1 << i)], F[S]) : upd(F[S - (1 << i)], F[S]);
}
void OR(int F[1 << (1 << 4)], bool rev = false) {
  for (int i = 0; i < (1 << 4); ++i)
    for (int S = 0; S < (1 << (1 << 4)); ++S)
      if (S & (1 << i))
        rev ? inc(F[S], F[S - (1 << i)]) : upd(F[S], F[S - (1 << i)]);
}
int dp(int l, int r) {
  int u = ++V, mid, Sum, a, b;
  if (l == r) {
    int ret = 0;
    for (char c = 'A'; c <= 'D'; ++c)
      if (S[l] == c || S[l] == '?') {
        ret = 0;
        for (int S = 0; S < (1 << 4); ++S)
          if (S & (1 << (c - 'A'))) ret |= (1 << S);
        ++F[u][ret];
      }
    for (char c = 'a'; c <= 'd'; ++c)
      if (S[l] == c || S[l] == '?') {
        ret = 0;
        for (int S = 0; S < (1 << 4); ++S)
          if ((S & (1 << (c - 'a'))) == 0) ret |= (1 << S);
        ++F[u][ret];
      }
    goto loop;
  }
  for (mid = l, Sum = 0; mid <= r; ++mid)
    if (!(Sum += (S[mid] == '(') - (S[mid] == ')'))) break;
  ++mid, a = dp(l + 1, mid - 2), b = dp(mid + 2, r - 1);
  if (S[mid] != '|') {
    AND(F[a]), AND(F[b]);
    for (int S = 0; S < (1 << (1 << 4)); ++S) temp[S] = Mult(F[a][S], F[b][S]);
    AND(F[a], true), AND(F[b], true);
    AND(temp, true);
    for (int S = 0; S < (1 << (1 << 4)); ++S) upd(F[u][S], temp[S]);
  }
  if (S[mid] != '&') {
    OR(F[a]), OR(F[b]);
    for (int S = 0; S < (1 << (1 << 4)); ++S) temp[S] = Mult(F[a][S], F[b][S]);
    OR(F[a], true), OR(F[b], true);
    OR(temp, true);
    for (int S = 0; S < (1 << (1 << 4)); ++S) upd(F[u][S], temp[S]);
  }
loop:
  return u;
}
int main() {
  scanf("%s", S + 1), N = strlen(S + 1);
  dp(1, N);
  int Q, Ans = 0;
  scanf("%d", &Q);
  for (int ret, t; Q--;) {
    ret = 0;
    for (int i = 0; i < 4; ++i) scanf("%d", &t), ret |= (t << i);
    scanf("%d", &t);
    for (int S = 0; S < (1 << (1 << 4)); ++S)
      if (((S & (1 << ret)) != 0) != t) F[1][S] = 0;
  }
  for (int S = 0; S < (1 << (1 << 4)); ++S) upd(Ans, F[1][S]);
  printf("%d", Ans);
  return 0;
}
