Language: C++

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>s
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <stdio.h>
#include <complex>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define MT(a,b,c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define MOD 1000000007

int DP[55][55][55][55];
int n[4],m;
map<string, int> msi;
map<int, vector<string>> mivs;
map<int, vector<char> > mivc;
int id[256];

vector<char> DFS(int k){
	if(EXIST(mivc, k))return mivc[k];
	int r=mivs[k].size();
	vector<char> ret;
	REP(i,r){
		if(mivs[k][i][0]=='['){
			int len = mivs[k][i].length();
			char t=0;
			FOR(j, 1, len-1)t|=(1<<id[mivs[k][i][j]]);
			ret.PB(t);
		}else{
			vector<char> vc=DFS(msi[mivs[k][i]]);
			ret.insert(ret.end(), ALL(vc));
		}
	}
	if(ret.size() > 400)ret.clear();
	return mivc[k]=ret;
}

int main(){
	id['A']=0;
	id['T']=1;
	id['G']=2;
	id['C']=3;
	REP(i,4)cin>>n[i];
	cin>>m;
	char buf[10000];
	cin.getline(buf, 10000);
	REP(i,m){
		cin.getline(buf, 10000);
		char *c=strtok(buf, ": ");
		msi[c]=i;
		while((c=strtok(NULL, " "))!=NULL)mivs[i].PB(c);
	}
	vector<char> vc=DFS(0);
	if(vc.size()==0 || vc.size()>n[0]+n[1]+n[2]+n[3]){
		cout << "0"<<endl;
		return 0;
	}
	DP[0][0][0][0]=1;
	int cidx[4];
	int ret = 0;
	for(cidx[0]=0;cidx[0]<=n[0];cidx[0]++){
		for(cidx[1]=0;cidx[1]<=n[1];cidx[1]++){
			for(cidx[2]=0;cidx[2]<=n[2];cidx[2]++){
				for(cidx[3]=0;cidx[3]<=n[3];cidx[3]++){
					int idx = cidx[0]+cidx[1]+cidx[2]+cidx[3];
					if(idx >= vc.size())break;
					int *adding = &DP[cidx[0]][cidx[1]][cidx[2]][cidx[3]];
					int *added;
					REP(p,4){
						if(cidx[p]<n[p] && (vc[idx] & (1<<p))){
							added = &DP[cidx[0]+(p==0)][cidx[1]+(p==1)][cidx[2]+(p==2)][cidx[3]+(p==3)];
							*added = ((*added)+(*adding))%MOD;
						}
					}
				}
			}
		}
	}
	REP(i,n[0]+1){
		REP(j,n[1]+1){
			REP(k,n[2]+1){
				int l=vc.size()-i-j-k;
				ret = (ret + DP[i][j][k][l])%MOD;
			}
		}
	}
	cout << ret << endl;
	return 0;
}

Compiler output:
a.cc:12:10: fatal error: sstream>: No such file or directory
   12 | #include <sstream>s
      |          ^~~~~~~~~~
compilation terminated.