1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,x,y) for(int i=(int)(x);i<(int)(y);++i)
#define FORE(i,x,y) for(int i=(int)(x);i<=(int)(y);++i)
#define FORD(i,x,y) for(int i=(int)(x);i>=(int)(y);--i)
#define PB push_back
#define MP make_pair
#define ST first
#define ND second
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN=1e4+6;
const ll MOD=1e9+7;

int n;
ll ans=0;
ll sil[MAXN];
string s[3];
ll r[3];
ll tsNw[MAXN];
inline ll Dif(string s1,string s2){
    ll res=0;
    FOR(i,0,n){
        if(s1[i]!=s2[i])++res;
    }
    return res;
}



ll gcdExtended(ll a, ll b, ll *x, ll *y){
    if(a==0){
        *x=0,*y=1;
        return b;
    }

    ll x1, y1;
    ll gcd=gcdExtended(b%a,a,&x1,&y1);


    *x=y1-(b/a)*x1;
    *y=x1;

    return gcd;
}

ll modInverse(ll b,ll m){
    ll x,y;
    ll g=gcdExtended(b,MOD,&x,&y);
    if (g != 1)
        return -1;
    return (x%MOD+MOD)%MOD;
}


ll modDivide(ll a, ll b){
    a%=MOD;
    ll inv=modInverse(b, MOD);
    if (inv==-1)return 1;
    else return (inv*a) % MOD;
}












ll Nw(ll v,ll w){
    return modDivide(sil[v],(sil[w]*sil[v-w]));
}
ll DoNw(ll v,ll w){
    ll res=0;
    w=min(w,v);
    for(;w>=0;--w){
        res+=Nw(v,w);
        res%=MOD;
    }
    return res;
}
ll pot(ll pod,ll wyk){
    ll res=1;
    while(wyk>0){
        if(wyk&1)res*=pod,res%=MOD;
        pod*=pod;
        pod%=MOD;
        wyk/=2;
    }
    return res;
}

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);
    cin>>n;
    FOR(i,0,3)cin>>r[i]>>s[i];
    sil[0]=1;
    for(ll i=1;i<=n;++i)sil[i]=(sil[i-1]*i)%MOD;
    int ile=3;
    if(r[0]==r[1]&&s[0]==s[1]){
        ile=2;
        r[1]=r[2];
        s[1]=s[2];
    }else if(r[0]==r[2]&&s[0]==s[2]){
        ile=2;
    }else if(r[1]==r[2]&&s[1]==s[2]){
        ile=2;
    }
    FOR(i,0,ile){
        ans+=DoNw(n,r[i]);
        ans%=MOD;
    }
    FOR(i,0,ile){
        FOR(j,i+1,ile){
            ll ds=Dif(s[i],s[j]);
            ll sum=r[i]+r[j];
            ll mi=r[i];
            ll mx=r[j];
            if(mx<mi)swap(mi,mx);
            if(sum-ds>=0){
                for(ll z=min(mx,ds);z+mi>=ds&&z>=0;--z){
                    ll x1=Nw(ds,z);//ktore sie wyrownoja przez mx (reszta mi)
                    ll x2=DoNw(n-ds,min(mx-z,mi-(ds-z)));//ktore dobre sie zamienajaa
                    //ll x3=Nw(n-d-(mx-z),mi-(d-z));
                    x1=(x1*x2)%MOD;
                    //x1=(x1*x3)%MOD;
                    ans-=x1;
                    ans%=MOD;
                }
            }
        }
    }
    if(ile==3){
        int d[3]={0,0,0};
        FOR(i,0,n){
            if(s[0][i]==s[1][i]&&s[0][i]!=s[2][i])++d[2];
            if(s[2][i]==s[0][i]&&s[2][i]!=s[1][i])++d[1];
            if(s[1][i]==s[2][i]&&s[1][i]!=s[0][i])++d[0];
        }
        ll ts=n-(d[0]+d[1]+d[2]);
        ll tr[3]={r[0],r[1],r[2]};
        tsNw[0]=1;
        FORE(i,1,ts){
            tsNw[i]=tsNw[i-1]+Nw(ts,i);
            tsNw[i]%=MOD;
        }
        FORE(i,0,d[0]){
            ll k0=Nw(d[0],i);
            if(r[0]-i<0)break;
            FORE(j,0,d[1]){
                ll k1=Nw(d[1],j);
                if(r[1]-(d[0]-i)-j<0)break;
                FORE(z,0,d[2]){
                    ll k2=Nw(d[2],z);
                    ll x0=r[0]-i-(d[1]-j)-(d[2]-z);
                    ll x1=r[1]-(d[0]-i)-j-(d[2]-z);
                    ll x2=r[2]-(d[0]-i)-(d[1]-j)-z;
                    if(x2<0)break;
                    if(x0<0||x1<0)continue;
                    ll x=min(x0,min(x1,x2));
                    ans+=(((tsNw[min(ts,x)]*k0)%MOD)*((k1*k2)%MOD))%MOD;
                    ans%=MOD;
                }
            }
        }
    }

    if(ans<0)ans+=MOD;
    cout<<ans<<"\n";
    return 0;
}