#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream& operator<<(ostream& o, const pair<T, U>& v) {
  return o << "(" << v.first << ", " << v.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  o << "{";
  for (int i = 0; i < (int)((int)((v).size())); ++i)
    o << (i ? ", " : "") << v[i];
  return o << "}";
}
template <class T, class U>
ostream& operator<<(ostream& o, const map<T, U>& v) {
  o << "{";
  for (const auto& e : v) o << e << ", ";
  return o << "}";
}
template <class T, class U>
ostream& operator<<(ostream& o, const unordered_map<T, U>& v) {
  o << "{";
  for (const auto& e : v) o << e << ", ";
  return o << "}";
}
template <class T>
ostream& operator<<(ostream& o, const set<T>& v) {
  o << "{";
  for (const auto& e : v) o << e << ", ";
  return o << "}";
}
template <class T>
string to_s(const T& v) {
  ostringstream is;
  is << v;
  return is.str();
}
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
const int dy[] = {1, 0, -1, 0, -1, 1, 1, -1, 0};
string s;
int pos;
const long long mod = 1e9 + 7;
const int nv = 4;
const int bit = 1 << nv;
int table[nv * 2][1 << bit];
int tableall[1 << bit];
void maket(char ch) {
  int a = tolower(ch) - 'a';
  int ut = a * 2 + (islower(ch) ? 1 : 0);
  for (int i = 0; i < (int)(1 << bit); ++i) {
    bool ok = true;
    for (int j = 0; j < (int)(1 << nv); ++j) {
      int va = 0;
      if (((j >> a) & 1) && isupper(ch))
        va = 1;
      else if (((~j >> a) & 1) && islower(ch))
        va = 1;
      ok &= ((i >> j) & 1) == va;
      if (!ok) break;
    }
    table[ut][i] += ok;
    tableall[i] += ok;
  }
}
long long dp[2][1 << bit];
vector<long long> mab(const vector<long long>& e) {
  for (int i = 0; i < (int)(1 << bit); ++i) dp[0][i] = e[i];
  for (int j = 1; j <= bit; ++j) {
    int ne = j & 1;
    int cur = !ne;
    for (int x = 0; x < (int)(1 << bit); ++x) {
      if ((x >> (j - 1)) & 1)
        dp[ne][x] = (dp[cur][x] + dp[cur][x ^ (1 << (j - 1))]) % mod;
      else
        dp[ne][x] = dp[cur][x];
    }
  }
  vector<long long> ret(1 << bit);
  for (int i = 0; i < (int)(1 << bit); ++i) ret[i] = dp[bit & 1][i];
  return ret;
}
void covpro(vector<long long>& ret, const vector<long long>& e1,
            const vector<long long>& e2) {
  vector<long long> ne1 = mab(e1);
  vector<long long> ne2 = mab(e2);
  for (int i = 0; i < (int)(1 << bit); ++i) dp[0][i] = ne1[i] * ne2[i] % mod;
  for (int j = 1; j <= bit; ++j) {
    int ne = j & 1;
    int cur = !ne;
    for (int x = 0; x < (int)(1 << bit); ++x) {
      if ((x >> (j - 1)) & 1) {
        dp[ne][x] = dp[cur][x] - dp[cur][x ^ (1 << (j - 1))];
        if (dp[ne][x] < 0) dp[ne][x] += mod;
      } else
        dp[ne][x] = dp[cur][x];
    }
  }
  for (int i = 0; i < (int)(1 << bit); ++i) ret[i] = dp[bit & 1][i];
  return;
  for (int i = 0; i < (int)(1 << bit); ++i) {
    for (int j = 0; j < (int)(1 << bit); ++j) {
      ret[i | j] += e1[i] * e2[j] % mod;
      ret[i | j] %= mod;
    }
  }
}
vector<long long> expr() {
  vector<long long> ret(1 << bit);
  if (s[pos] == '1') {
    ++pos;
    return vector<long long>(1 << bit, 1);
  }
  if (s[pos] == '0') {
    ++pos;
    return ret;
  }
  if (isalpha(s[pos])) {
    int a = tolower(s[pos]) - 'a';
    int ut = a * 2 + (islower(s[pos]) ? 1 : 0);
    for (int j = 0; j < (int)(1 << bit); ++j) ret[j] = table[ut][j];
    ++pos;
    return ret;
  } else if (s[pos] == '?') {
    for (int j = 0; j < (int)(1 << bit); ++j) ret[j] = tableall[j];
    ++pos;
    return ret;
  }
  ++pos;
  auto e1 = expr();
  ++pos;
  char op = s[pos];
  ++pos;
  ++pos;
  auto e2 = expr();
  ++pos;
  if (op == '|' || op == '?') {
    covpro(ret, e1, e2);
  }
  if (op == '&' || op == '?') {
    vector<long long> nr(1 << bit), ne1(1 << bit), ne2(1 << bit);
    int ma = (1 << bit) - 1;
    for (int i = 0; i < (int)(1 << bit); ++i) {
      ne1[i] = e1[~i & ma];
      ne2[i] = e2[~i & ma];
    }
    covpro(nr, ne1, ne2);
    for (int i = 0; i < (int)(1 << bit); ++i) {
      ret[i] += nr[~i & ma];
      if (ret[i] >= mod) ret[i] -= mod;
    }
  }
  return ret;
}
int main(int argc, char* argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  for (int i = 0; i < (int)(nv); ++i) {
    maket(i + 'a');
    maket(i + 'A');
  }
  cin >> s;
  auto a = expr();
  int n;
  cin >> n;
  vector<pair<int, int> > chk;
  for (int j = 0; j < (int)(n); ++j) {
    int p = 0;
    for (int k = 0; k < (int)(nv); ++k) {
      int t;
      cin >> t;
      p += t << k;
    }
    int v;
    cin >> v;
    chk.emplace_back(p, v);
  }
  long long ans = 0;
  for (int i = 0; i < (int)(1 << bit); ++i) {
    bool ok = true;
    for (auto e : chk) {
      ok &= ((i >> e.first) & 1) == e.second;
      if (!ok) break;
    }
    if (ok) ans = (ans + a[i]) % mod;
  }
  cout << ans << endl;
}
