#include <bits/stdc++.h>
#define MOD (long long)(1e9 + 7)
using namespace std;

template <int mod>
struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t y)
      : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const {
    return ModInt(*this) += p;
  }
  ModInt operator-(const ModInt &p) const {
    return ModInt(*this) -= p;
  }
  ModInt operator*(const ModInt &p) const {
    return ModInt(*this) *= p;
  }
  ModInt operator/(const ModInt &p) const {
    return ModInt(*this) /= p;
  }
  bool operator==(const ModInt &p) const {
    return x == p.x;
  }
  bool operator!=(const ModInt &p) const {
    return x != p.x;
  }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(int64_t n) const {
    ModInt ans(1), mul(x);
    while(n) {
      if(n & 1) ans *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ans;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};

int c[4] = {}, m, allsum = 0;
vector<string> s;
map<string, vector<int>> cnt;
vector<int> memo;

ModInt<MOD> solve();
int char_to_int(char x) {
  if(x == 'A') return 0;
  if(x == 'T') return 1;
  if(x == 'G') return 2;
  if(x == 'C') return 3;
  assert(0);
}

int main() {
  for(int i = 0; i < 4; ++i) {
    cin >> c[i];
    allsum += c[i];
  }
  cin >> m;
  s.resize(m);
  getline(cin, s[0]);
  for(int i = 0; i < m; ++i) getline(cin, s[i]);
  cout << solve() << endl;
  return 0;
}

ModInt<MOD> solve() {
  for(int i = m - 1; i >= 0; --i) {
    int id = 0, ssize = s[i].size();
    string t;
    vector<int> nowcnt(1 << 4, 0);
    while(s[i][id] != ':') t += s[i][id++];
    assert(s[i][id] == ':');
    ++id;

    // space -> ++id
    while(++id < ssize) {
      // A T G C
      if(s[i][id] == '[') {
        int bit = 0;
        ++id;
        while(s[i][id] != ']')
          bit += 1LL << char_to_int(s[i][id++]);
        ++nowcnt[bit];
        assert(s[i][id] == ']');
        ++id;
      }
      else {
        string tos;
        while(id < ssize && isalpha(s[i][id]))
          tos += s[i][id++];
        // assert(cnt.find(tos) != cnt.end());
        vector<int> tmp = cnt[tos];
        int sum = 0;
        if(nowcnt[0] >= 0 && tmp[0] >= 0) {
          for(int j = 0; j < (1 << 4); ++j) {
            nowcnt[j] += tmp[j];
            sum += nowcnt[j];
            if(allsum < sum) {
              nowcnt[0] = -1;
              break;
            }
          }
        }
        else
          nowcnt[0] = -1;
      }
    }
    cnt[t] = nowcnt;
    if(i == 0) {
      if(nowcnt[0] < 0) return 0;
      int sum = 0;
      for(int j = 1; j < (1 << 4); ++j) sum += nowcnt[j];
      if(sum != allsum) return 0;
      memo = nowcnt;
    }
  }
  allsum = 0;
  vector<vector<vector<vector<ModInt<MOD>>>>> dp;
  dp.assign(2,
            vector<vector<vector<ModInt<MOD>>>>(
                c[0] + 1, vector<vector<ModInt<MOD>>>(
                              c[1] + 1, vector<ModInt<MOD>>(
                                            c[2] + 1, 0))));
  dp[0][0][0][0] = 1;
  for(int i = 0; i < (1 << 4); ++i) {
    for(int j = 0; j < memo[i]; ++j) {
      dp[(allsum + 1) % 2].assign(
          c[0] + 1,
          vector<vector<ModInt<MOD>>>(
              c[1] + 1, vector<ModInt<MOD>>(c[2] + 1, 0)));
      for(int A = 0; A <= c[0]; ++A)
        for(int T = 0; T <= c[1]; ++T)
          for(int G = 0; G <= c[2]; ++G) {
            for(int k = 0; k < 4; ++k)
              if(i >> k & 1) {
                if(k == 0 && A > 0)
                  dp[(allsum + 1) % 2][A][T][G] +=
                      dp[allsum % 2][A - 1][T][G];
                if(k == 1 && T > 0)
                  dp[(allsum + 1) % 2][A][T][G] +=
                      dp[allsum % 2][A][T - 1][G];
                if(k == 2 && G > 0)
                  dp[(allsum + 1) % 2][A][T][G] +=
                      dp[allsum % 2][A][T][G - 1];
                if(k == 3)
                  dp[(allsum + 1) % 2][A][T][G] +=
                      dp[allsum % 2][A][T][G];
              }
          }
      ++allsum;
    }
  }
  return dp[allsum % 2][c[0]][c[1]][c[2]];
}


Measured memory_bytes: 4632