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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL; typedef unsigned long long ULL;

class Input {
 public:
  Input() { bufpos = bufend = buffer; eof = false; }
  bool Eof() { return eof; }
  char Peek() { if(bufpos == bufend) Grab(); return *bufpos; }
  unsigned char UPeek() { return static_cast<unsigned char>(Peek()); }
  void SkipWS();
  template<class T> T Get();
  void operator()() {}
  template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) {
    arg = Get<Arg>();
    operator()(args...);
  }
 private:
  static const int BUFSIZE = 1<<16;
  char buffer[BUFSIZE];
  char *bufpos;
  char *bufend;
  bool eof;
  void Grab();
};

void Input::Grab() {
  if(eof) return;
  bufpos = buffer;
  bufend = buffer + read(0, buffer, BUFSIZE);
  if(bufend==bufpos) { eof=true; *bufpos=0; }
}

template<> inline char Input::Get<char>() {
  char res = Peek();
  ++bufpos;
  return res;
}

void Input::SkipWS() {
  while(isspace(UPeek())) Get<char>();
}

template<> unsigned Input::Get<unsigned>() {
  SkipWS();
  unsigned x = 0;
  while(isdigit(UPeek())) {
    x = 10u * x + (Get<char>()-'0');
  }
  return x;
}

template<> int Input::Get<int>() {
  SkipWS();
  bool neg = false;
  if(Peek()=='-') { neg=true; Get<char>(); }
  unsigned x = Get<unsigned>();
  if (neg) x = -x;
  return static_cast<int>(x);
}

Input IN;

template<unsigned MOD>
class Modulo {
  unsigned v;
 public:
  Modulo(unsigned x=0):v(x) {}
  unsigned get() const { return v; }
  Modulo operator+(Modulo b) const {
    unsigned res = v+b.v;
    if (res >= MOD) res -= MOD;
    return res;
  }
  void operator+=(Modulo b) { *this = *this + b; }
  Modulo operator-(Modulo b) const { return *this + Modulo(MOD-b.v); }
  void operator-=(Modulo b) { *this = *this - b; }
  Modulo operator*(Modulo b) const { return Modulo(ULL(v) * ULL(b.v) % MOD); }
  void operator*=(Modulo b) { *this = *this * b; }
};

// [0..size1-1][base2 .. base2+size2-1]
template <typename T>
class Array2d {
 public:
  Array2d(int _size1, int _size2, int _base2) :
    size1(_size1),
    size2(_size2),
    base2(_base2),
    data(size1 * size2)
  {}

  T &operator()(int i, int j) {
    return data[i * size2 + j - base2];
  }
 private:
  int size1;
  int size2;
  int base2;
  vector<T> data;
};

using Mod = Modulo<1'000'000'007>;

void read_input(int &a, int &b, int &c, int &d, int &r1, int &r2, int &r3) {
  int n; IN(n);
  string text[3];
  int r[3];
  REP(i,3) {
    IN(r[i]);
    IN.SkipWS();
    text[i].resize(n);
    REP(j,n) {
      char c;
      IN(c);
      assert(c=='0' || c=='1');
      text[i][j] = c;
    }
  }
  int cnt[2][2] = {};
  REP(i,n) {
    ++cnt[text[0][i] != text[1][i]][text[0][i] != text[2][i]];
  }

  r1 = r[0];
  r2 = r[1];
  r3 = r[2];
  a = cnt[0][0];
  b = cnt[1][0];
  c = cnt[0][1];
  d = cnt[1][1];
}

vector<vector<Mod>> binomial_table;

void compute_binomial_table(vector<int> ns) {
  sort(ns.begin(), ns.end());
  assert(!ns.empty());
  binomial_table.resize(ns.back() + 1);

  vector<Mod> row;
  vector<Mod> next_row;
  row.reserve(ns.back()+1);
  next_row.reserve(ns.back()+1);

  int n = 0;
  row = {Mod{1}};

  auto next = ns.begin();
  while (next != ns.end()) {
    if (*next == n) {
      binomial_table[n] = row;
      while (next != ns.end() && *next == n) ++next;
    }

    ++n;
    next_row.resize(n+1);
    next_row[0] = Mod{1};
    FOR(k,1,n-1) next_row[k] = row[k-1] + row[k];
    next_row[n] = Mod{1};

    swap(row, next_row);
  }
}

inline Mod binomial(int n, int k) {
  return k < 0 || k > n ? Mod{0} : binomial_table[n][k];
}

Mod One(int a, int r) {
  Mod res = 0;
  FOR(x,0,r) res += binomial(a,x);
  return res;
}

Mod Two(int a, int b, int r1, int r2) {
  // sum_x[A] = sum_(x<A) binomial(a,x)
  // 0 <= x <= a so 0 <= A <= a+1
  vector<Mod> sum_x(a+2);
  sum_x[0] = 0;
  FOR(A, 1, a+1) sum_x[A] = sum_x[A-1] + binomial(a, A-1);

  Mod res = 0;
  FOR(y,0,b) {
    int A = min(r1-y, r2-b+y)+1;
    if (A<0) A=0;
    if (A>a+1) A=a+1;
    res += binomial(b,y) * sum_x[A];
  }
  return res;
}

Mod Three(int a, int b, int c, int d, int r1, int r2, int r3) {
  // sum_xy(A,B) = sum_(x+y < A, x-y < B) binomial(a,x) * binomial(b,y)
  // 0 <= x+y <= a+b, so 0 <= A <= a+b+1
  // -b <= x-y <= a, so -b <= B <= a+1
  Array2d<Mod> sum_xy(a+b+2, a+b+2, -b);

  FOR(A, 1, a+b+1) {
    FOR(B, -b+1, a+1) {
      Mod s = sum_xy(A-1, B);
      s += sum_xy(A, B-1);
      s -= sum_xy(A-1, B-1);
      if (((A+B)&1) == 0) {
        // x+y = A-1
        // x-y = B-1
        int x = (A+B-2)>>1;
        int y = (A-B)>>1;
        s += binomial(a,x) * binomial(b,y);
      }
      sum_xy(A, B) = s;
    }
  }

  Mod res = 0;
  FOR(z, 0, c) {
    Mod sum_t = 0;
    FOR(t, 0, d) {
      int A = min(r1-z-t, r3-c-d+z+t) + 1;
      if (A<0) A=0;
      if (A>a+b+1) A=a+b+1;
      int B = r2-b-d-z+t + 1;
      if (B<-b) B=-b;
      if (B>a+1) B=a+1;
      sum_t += binomial(d, t) * sum_xy(A, B);
    }
    res += binomial(c, z) * sum_t;
  }

  return res;
}

int main() {
  int a,b,c,d;
  int r1,r2,r3;
  read_input(a,b,c,d,r1,r2,r3);
  compute_binomial_table({a,b,c,d,a+b,a+c,a+d,b+c,b+d,c+d,a+b+c+d});
  Mod result = 0;
  result += One(a+b+c+d,r1);
  result += One(a+b+c+d,r2);
  result += One(a+b+c+d,r3);
  result -= Two(a+c,b+d,r1,r2);
  result -= Two(a+b,c+d,r1,r3);
  result -= Two(a+d,b+c,r2,r3);
  result += Three(a,b,c,d,r1,r2,r3);
  cout << result.get() << '\n';
}