#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
const ll mod = 1000000007;
int N[4];
int K;
string dust;
vector<string> Parse[50];
vector<string> parse(string S) {
    vector<string> ret;
    S.push_back(' ');
    string tmp = "";
    for(int i = 0; i < S.size(); i++) {
        if(S[i] == ' ') {
            ret.push_back(tmp);
            tmp = "";
        } else {
            tmp.push_back(S[i]);
        }
    }
    return ret;
}
vector<int> v[100];
map<string, int> mp;

void PUSH(vector<int> &a, vector<int> b) {
    for(int i = 0; i < b.size(); i++) {
        a.push_back(b[i]);
    }
}

string ATGC = "ATGC";
ll dp[60][60][60][60];

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    int Nsum = 0;
    for(int i = 0; i < 4; i++) {
        cin >> N[i];
        Nsum += N[i];
    }
    cin >> K;
    getline(cin, dust);
    for(int i = 0; i < K; i++) {
        getline(cin, dust);
        Parse[i] = parse(dust);
        Parse[i][0].pop_back();
        //for(auto tmp : Parse[i]) cerr << tmp << " ";
        //cerr << endl;
    }
    for(int i = K - 1; i >= 0; i--) {
        mp[Parse[i][0]] = i;
        for(int j = 1; j < Parse[i].size(); j++) {
            //cerr << i << " " << j << endl;
            if(Parse[i][j][0] != '[') {
                //cerr << Parse[i][j] << endl;
                //cerr << mp[Parse[i][j]] << endl;
                PUSH(v[i], v[mp[Parse[i][j]]]);
                //cerr << Parse[i][j] << endl;
            } else {
                int nxt = 0;
                for(int k = 1; k + 1 < Parse[i][j].size(); k++) {
                    for(int l = 0; l < 4; l++) {
                        if(Parse[i][j][k] == ATGC[l]) nxt |= (1 << l);
                    }
                }
                v[i].push_back(nxt);
            }
        }
        if(v[i].size() > Nsum) {
            cout << 0 << endl;
            return 0;
        }
    }
    /*
    for(int i = 0; i < v[0].size(); i++) {
        cerr << v[0][i] << " ";
    }
    cerr << endl;
    */
    if(v[0].size() != Nsum) {
        cout << 0 << endl;
        return 0;
    }
    dp[0][0][0][0] = 1;
    for(int a = 0; a <= N[0]; a++) {
        for(int b = 0; b <= N[1]; b++) {
            for(int c = 0; c <= N[2]; c++) {
                for(int d = 0; d <= N[3]; d++) {
                    if(a+b+c+d==Nsum) break;
                    int idx = a + b + c + d;
                    if(v[0][idx] & 1) {
                        dp[a+1][b][c][d] += dp[a][b][c][d];
                        dp[a+1][b][c][d] %= mod;
                    }
                    if(v[0][idx] & 2) {
                        dp[a][b+1][c][d] += dp[a][b][c][d];
                        dp[a][b+1][c][d] %= mod;
                    }
                    if(v[0][idx] & 4) {
                        dp[a][b][c+1][d] += dp[a][b][c][d];
                        dp[a][b][c+1][d] %= mod;
                    }
                    if(v[0][idx] & 8) {
                        dp[a][b][c][d+1] += dp[a][b][c][d];
                        dp[a][b][c][d+1] %= mod;
                    }
                }
            }
        }
    }
    cout << dp[N[0]][N[1]][N[2]][N[3]] << endl;
    return 0;
}


Measured memory_bytes: 74240