#if defined(_USE_PCH_)
#include "pch.hpp"
#else
#include <bits/stdc++.h>
#endif
#define RNG(V_, A_, B_, ...) for(int V_=(A_), V_##_END=(B_) __VA_OPT__(,) __VA_ARGS__; V_<=V_##_END; V_++)
#define IRNG(V_, A_, B_, ...) for(int V_=(A_), V_##_END=(B_) __VA_OPT__(,) __VA_ARGS__; V_>=V_##_END; V_--)
#ifdef _WIN32
#define long int64_t
#endif
#define fi first
#define se second
#define _UN using namespace
using namespace std;
const int MOD=1e9+7;
typedef pair<int,int> pii;
int n,qn; pair<int,int> A[21];
int id[4][14],sum[1048576];
bool G[1048576];
long F[1048576];
long qpow(long x,int p){
    long ret=1;
    for(; p; p>>=1,x=x*x%MOD){
        if(p&1) ret=ret*x%MOD;
    }
    return ret;
}
long inv[21];
int main(){
#if defined(_LOCAL_)
    freopen("in","r",stdin);
//  freopen("out","w",stdout);
//  freopen("/dev/null","w",stderr);
#else
    freopen("card.in","r",stdin);
    freopen("card.out","w",stdout);
#endif
    ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
    cin>>n>>qn;
    RNG(i,1,n) inv[i]=qpow(i,MOD-2);
    RNG(i,1,n){
        char s[4]; cin>>s;
        A[i]={(s[0]=='K'?0:s[0]=='R'?1:s[0]=='B'?2:3),atoi(s+1)};
        id[A[i].fi][A[i].se]=i;
    }
    RNG(S,1,(1<<n)-1) sum[S]=sum[S^(S&-S)]+A[__builtin_ctz(S&-S)+1].se;
    {
        static int B[1000]; int k;
        RNG(i,1,13){
            if(id[0][i]&&id[1][i]&&id[2][i]) B[++k]=1<<(id[0][i]-1)|1<<(id[1][i]-1)|1<<(id[2][i]-1);
            if(id[0][i]&&id[1][i]&&id[3][i]) B[++k]=1<<(id[0][i]-1)|1<<(id[1][i]-1)|1<<(id[3][i]-1);
            if(id[0][i]&&id[2][i]&&id[3][i]) B[++k]=1<<(id[0][i]-1)|1<<(id[2][i]-1)|1<<(id[3][i]-1);
            if(id[1][i]&&id[2][i]&&id[3][i]) B[++k]=1<<(id[1][i]-1)|1<<(id[2][i]-1)|1<<(id[3][i]-1);
            if(id[0][i]&&id[1][i]&&id[2][i]&&id[3][i]) B[++k]=1<<(id[0][i]-1)|1<<(id[1][i]-1)|1<<(id[2][i]-1)|1<<(id[3][i]-1);
        }
        RNG(i,0,3){
            RNG(j,1,11){
                if(id[i][j]&&id[i][j+1]&&id[i][j+2]){
                    B[++k]=1<<(id[i][j]-1)|1<<(id[i][j+1]-1)|1<<(id[i][j+2]-1);
                    if(j+3<=13&&id[i][j+3]){
                        k++,B[k]=B[k-1]|1<<(id[i][j+3]-1);
                        if(j+4<=13&&id[i][j+4]) k++,B[k]=B[k-1]|1<<(id[i][j+4]-1);
                    }
                }
            }
        }
        G[0]=1;
        RNG(i,1,k){
            auto x=B[i];
            for(int msk=((1<<n)-1)^x,S=msk; ; S=(S-1)&msk){
                G[S|x]|=G[S];
                if(!S) break;
            }
        }
    }
    RNG(_,1,qn){
        int l,S0=0; cin>>l;
        RNG(_1,1,l,i) cin>>i,S0|=1<<(i-1);
        if(G[S0]){ cout<<sum[((1<<n)-1)^S0]<<"\n"; continue; }
        F[S0]=1;
        static int ord[1024]; int k=0;
        for(int msk=((1<<n)-1)^S0,S=msk; S; S=(S-1)&msk) ord[k++]=S|S0;
        long ans=0;
        IRNG(p,k-1,0){
            auto S=ord[p];
            F[S]=0;
            RNG(i,1,n){
                if(((S&~S0)>>(i-1))&1) F[S]=(F[S]+F[S^(1<<(i-1))])%MOD;
            }
            F[S]=F[S]*inv[__builtin_popcount(S)]%MOD;
            if(G[S]) ans=(ans+F[S]*sum[((1<<n)-1)^S])%MOD,F[S]=0;
        }
        cout<<ans<<"\n";
    }
}