#include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define PLL pair<LL, LL> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define pb pop_back #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define Sim template<class n Sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} Sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} Sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} Sim, class s> void mini(n &p, s y) {if(p>y) p = y;} Sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " = ", (x), " " #define A(a, i) #a "[" #i " = ", i, "] = ", a[i], " " const int M = 1024 * 16; const LL mod = 1e9 + 7; LL sil[M], odw[M], res; int n, r[3], inne[3]; char C[3][M]; LL poww(LL x, LL e) { if (e == 0) { return 1; } if (e % 2 == 0) { return poww(x * x % mod, e / 2); } return x * poww(x, e - 1) % mod; } LL newt(int a, int b) { assert(a >= b); return sil[a] * odw[b] % mod * odw[a - b] % mod; } LL solve2(int r1, int r2, int wsp, int inn) { LL rr = 0; vector<LL> V; LL s = 0; FOR(i, 0, wsp) { V.PB(newt(wsp, i)); V.back() += s; V.back() %= mod; s = V.back(); } FOR(i, 0, inn) { int ind = min(r1 - i, r2 - inn + i); mini(ind, wsp); if (ind >= 0) { rr = (rr + newt(inn, i) * V[ind]) % mod; } } return rr; } vector<vector<LL>> A; LL solve3() { LL rr = 0; int wsp = n - inne[0] - inne[1] - inne[2]; int wym = wsp + inne[2]; A.resize(wym + 1); FOR(i, 0, wym) { A[i].resize(wym + 1); } vector<LL> nwsp, nin; FOR(i, 0, wsp) { nwsp.PB(newt(wsp, i)); } FOR(i, 0, inne[2]) { nin.PB(newt(inne[2], i)); } FOR(i, 0, wsp) { FOR(j, 0, inne[2]) { A[i + j][i - j + inne[2]] = nwsp[i] * nin[j] % mod; } } FOR(i, 1, wym) { A[0][i] += A[0][i - 1]; if (A[0][i] >= mod) { A[0][i] -= mod; } } FOR(i, 1, wym) { A[i][0] += A[i - 1][0]; if (A[i][0] >= mod) { A[i][0] -= mod; } FOR(j, 1, wym) { A[i][j] += A[i - 1][j] + A[i][j - 1] - A[i - 1][j - 1]; while (A[i][j] >= mod) { A[i][j] -= mod; } while (A[i][j] <= 0) { A[i][j] += mod; } } } vector<LL> n1, n2; FOR(i, 0, inne[0]) { n1.PB(newt(inne[0], i)); } FOR(i, 0, inne[1]) { n2.PB(newt(inne[1], i)); } FOR(i, 0, inne[0]) { FOR(j, 0, inne[1]) { int r0 = r[0] - j - (inne[0] - i); int r1 = r[1] - i - (inne[1] - j); int r2 = r[2] - i - j; if (r0 < 0 || r1 < 0 || r2 < 0) { continue; } int mxsum = min(r0, r1); int mxdiff = r2 - inne[2]; int plus = min(wym, mxsum); int minus = min(wym, mxdiff + inne[2]); rr = (rr + n1[i] * n2[j] % mod * A[plus][minus]) % mod; } } return rr; } int32_t main() { //ios_base::sync_with_stdio(0); //cin.tie(NULL); sil[0] = 1; odw[0] = 1; FOR(i, 1, M - 1) { sil[i] = sil[i - 1] * i % mod; odw[i] = poww(sil[i], mod - 2); } scanf("%d", &n); FOR(i, 0, 2) { scanf("%d%s", r + i, C[i]); FOR(j, 0, r[i]) { res += newt(n, j); } } res %= mod; FOR(i, 0, 2) { int wsp = 0; FOR(j, 0, n - 1) { if (C[(i + 1) % 3][j] == C[(i + 2) % 3][j]) { wsp++; } } res -= solve2(r[(i + 1) % 3], r[(i + 2) % 3], wsp, n - wsp); if (res < 0) { res += mod; } } FOR(i, 0, 2) { FOR(j, 0, n - 1) { if (C[i][j] != C[(i + 1) % 3][j] && C[i][j] != C[(i + 2) % 3][j]) { inne[i]++; } } } res += solve3(); if (res >= mod) { res -= mod; } printf("%lld\n", res); return 0; }
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | #include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define PLL pair<LL, LL> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define pb pop_back #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define Sim template<class n Sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} Sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} Sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} Sim, class s> void mini(n &p, s y) {if(p>y) p = y;} Sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " = ", (x), " " #define A(a, i) #a "[" #i " = ", i, "] = ", a[i], " " const int M = 1024 * 16; const LL mod = 1e9 + 7; LL sil[M], odw[M], res; int n, r[3], inne[3]; char C[3][M]; LL poww(LL x, LL e) { if (e == 0) { return 1; } if (e % 2 == 0) { return poww(x * x % mod, e / 2); } return x * poww(x, e - 1) % mod; } LL newt(int a, int b) { assert(a >= b); return sil[a] * odw[b] % mod * odw[a - b] % mod; } LL solve2(int r1, int r2, int wsp, int inn) { LL rr = 0; vector<LL> V; LL s = 0; FOR(i, 0, wsp) { V.PB(newt(wsp, i)); V.back() += s; V.back() %= mod; s = V.back(); } FOR(i, 0, inn) { int ind = min(r1 - i, r2 - inn + i); mini(ind, wsp); if (ind >= 0) { rr = (rr + newt(inn, i) * V[ind]) % mod; } } return rr; } vector<vector<LL>> A; LL solve3() { LL rr = 0; int wsp = n - inne[0] - inne[1] - inne[2]; int wym = wsp + inne[2]; A.resize(wym + 1); FOR(i, 0, wym) { A[i].resize(wym + 1); } vector<LL> nwsp, nin; FOR(i, 0, wsp) { nwsp.PB(newt(wsp, i)); } FOR(i, 0, inne[2]) { nin.PB(newt(inne[2], i)); } FOR(i, 0, wsp) { FOR(j, 0, inne[2]) { A[i + j][i - j + inne[2]] = nwsp[i] * nin[j] % mod; } } FOR(i, 1, wym) { A[0][i] += A[0][i - 1]; if (A[0][i] >= mod) { A[0][i] -= mod; } } FOR(i, 1, wym) { A[i][0] += A[i - 1][0]; if (A[i][0] >= mod) { A[i][0] -= mod; } FOR(j, 1, wym) { A[i][j] += A[i - 1][j] + A[i][j - 1] - A[i - 1][j - 1]; while (A[i][j] >= mod) { A[i][j] -= mod; } while (A[i][j] <= 0) { A[i][j] += mod; } } } vector<LL> n1, n2; FOR(i, 0, inne[0]) { n1.PB(newt(inne[0], i)); } FOR(i, 0, inne[1]) { n2.PB(newt(inne[1], i)); } FOR(i, 0, inne[0]) { FOR(j, 0, inne[1]) { int r0 = r[0] - j - (inne[0] - i); int r1 = r[1] - i - (inne[1] - j); int r2 = r[2] - i - j; if (r0 < 0 || r1 < 0 || r2 < 0) { continue; } int mxsum = min(r0, r1); int mxdiff = r2 - inne[2]; int plus = min(wym, mxsum); int minus = min(wym, mxdiff + inne[2]); rr = (rr + n1[i] * n2[j] % mod * A[plus][minus]) % mod; } } return rr; } int32_t main() { //ios_base::sync_with_stdio(0); //cin.tie(NULL); sil[0] = 1; odw[0] = 1; FOR(i, 1, M - 1) { sil[i] = sil[i - 1] * i % mod; odw[i] = poww(sil[i], mod - 2); } scanf("%d", &n); FOR(i, 0, 2) { scanf("%d%s", r + i, C[i]); FOR(j, 0, r[i]) { res += newt(n, j); } } res %= mod; FOR(i, 0, 2) { int wsp = 0; FOR(j, 0, n - 1) { if (C[(i + 1) % 3][j] == C[(i + 2) % 3][j]) { wsp++; } } res -= solve2(r[(i + 1) % 3], r[(i + 2) % 3], wsp, n - wsp); if (res < 0) { res += mod; } } FOR(i, 0, 2) { FOR(j, 0, n - 1) { if (C[i][j] != C[(i + 1) % 3][j] && C[i][j] != C[(i + 2) % 3][j]) { inne[i]++; } } } res += solve3(); if (res >= mod) { res -= mod; } printf("%lld\n", res); return 0; } |