#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
string s;
int q;
int equations[16][5];
string low = "abcd";
string high = "ABCD";
int sumef[(1 << 16)][17];
int sumec[(1 << 16)][17];
int fc[(1 << 16)];
int val[(1 << 16)][17];
int ans[(1 << 16)];
int* solve(int l, int r) {
  int* h = new int[(1 << 16)];
  for (int g = 0; g < (1 << 16); g++) h[g] = 0;
  if (l == r) {
    if (s[l] != '?') {
      int fe = low.find(s[l]), se = high.find(s[l]);
      if (fe == -1) {
        int nu = 0;
        for (int y = 0; y < q; y++) {
          nu |= (equations[y][se] << y);
        }
        h[nu]++;
      } else {
        int nu = 0;
        for (int y = 0; y < q; y++) {
          nu |= ((!equations[y][fe]) << y);
        }
        h[nu]++;
      }
      return h;
    }
    for (int g = 0; g < 4; g++) {
      int nu = 0;
      for (int y = 0; y < q; y++) {
        nu |= (equations[y][g] << y);
      }
      h[nu]++;
    }
    for (int g = 0; g < 4; g++) {
      int nu = 0;
      for (int y = 0; y < q; y++) {
        nu |= ((!equations[y][g]) << y);
      }
      h[nu]++;
    }
    return h;
  }
  int open = 1;
  int op = -1;
  for (int g = l + 1; g <= r; g++) {
    if (s[g] == '(')
      open++;
    else if (s[g] == ')') {
      open--;
    }
    if (open == 0) {
      op = g + 1;
      break;
    }
  }
  int* f = solve(l + 1, op - 2);
  int* c = solve(op + 2, r - 1);
  if (s[op] != '&') {
    memset(sumef, 0, sizeof(sumef));
    memset(sumec, 0, sizeof(sumec));
    memset(fc, 0, sizeof(fc));
    memset(val, 0, sizeof(val));
    memset(ans, 0, sizeof(ans));
    for (int g = 0; g < (1 << q); g++) sumef[g][q] = f[g], sumec[g][q] = c[g];
    for (int g = 0; g < (1 << q); g++) {
      for (int y = q - 1; y >= 0; y--) {
        sumef[g][y] = sumef[g][y + 1];
        if ((1 << y) & g) sumef[g][y] += sumef[g ^ (1 << y)][y + 1];
        if (sumef[g][y] >= MOD) sumef[g][y] -= MOD;
      }
    }
    for (int g = 0; g < (1 << q); g++) {
      for (int y = q - 1; y >= 0; y--) {
        sumec[g][y] = sumec[g][y + 1];
        if ((1 << y) & g) sumec[g][y] += sumec[g ^ (1 << y)][y + 1];
        if (sumec[g][y] >= MOD) sumec[g][y] -= MOD;
      }
    }
    for (int g = 0; g < (1 << q); g++)
      fc[g] = (1LL * sumef[g][0] * sumec[g][0]) % MOD;
    for (int g = 0; g < (1 << q); g++) {
      for (int y = q - 1; y >= 0; y--) {
        val[g][y] = val[g][y + 1];
        if ((1 << y) & g) {
          val[g][y] = (val[g][y] + val[g ^ (1 << y)][y + 1]) % MOD;
          val[g][y] = (val[g][y] + ans[g ^ (1 << y)]) % MOD;
        }
      }
      ans[g] = (fc[g] - val[g][0]) % MOD;
      ans[g] += MOD;
      ans[g] %= MOD;
    }
    for (int g = 0; g < (1 << q); g++) h[g] = (h[g] + ans[g]) % MOD;
  }
  if (s[op] != '|') {
    memset(sumef, 0, sizeof(sumef));
    memset(sumec, 0, sizeof(sumec));
    memset(fc, 0, sizeof(fc));
    memset(val, 0, sizeof(val));
    memset(ans, 0, sizeof(ans));
    for (int g = 0; g < (1 << q); g++) sumef[g][q] = f[g], sumec[g][q] = c[g];
    for (int g = (1 << q) - 1; g >= 0; g--) {
      for (int y = q - 1; y >= 0; y--) {
        sumef[g][y] = sumef[g][y + 1];
        if (!((1 << y) & g)) sumef[g][y] += sumef[g ^ (1 << y)][y + 1];
        if (sumef[g][y] >= MOD) sumef[g][y] -= MOD;
      }
    }
    for (int g = (1 << q) - 1; g >= 0; g--) {
      for (int y = q - 1; y >= 0; y--) {
        sumec[g][y] = sumec[g][y + 1];
        if (!((1 << y) & g)) sumec[g][y] += sumec[g ^ (1 << y)][y + 1];
        if (sumec[g][y] >= MOD) sumec[g][y] -= MOD;
      }
    }
    for (int g = 0; g < (1 << q); g++)
      fc[g] = (1LL * sumef[g][0] * sumec[g][0]) % MOD;
    for (int g = (1 << q) - 1; g >= 0; g--) {
      for (int y = q - 1; y >= 0; y--) {
        val[g][y] = val[g][y + 1];
        if (!((1 << y) & g)) {
          val[g][y] = (val[g][y] + val[g ^ (1 << y)][y + 1]) % MOD;
          val[g][y] = (val[g][y] + ans[g ^ (1 << y)]) % MOD;
        }
      }
      ans[g] = (fc[g] - val[g][0]) % MOD;
      ans[g] += MOD;
      ans[g] %= MOD;
    }
    for (int g = 0; g < (1 << q); g++) h[g] = (h[g] + ans[g]) % MOD;
  }
  return h;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> s;
  cin >> q;
  for (int g = 0; g < q; g++) {
    for (int y = 0; y < 5; y++) cin >> equations[g][y];
  }
  int* ans = solve(0, s.length() - 1);
  int num = 0;
  for (int g = 0; g < q; g++)
    if (equations[g][4]) num |= (1 << g);
  cout << ans[num] << '\n';
  return 0;
}
