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
#include <stdio.h>
#include <string.h>

#define MAX_N 10002
#define MOD 1000000007

int n, max = 0;
int t0 = 0, t1 = 0, t2 = 0, t3 = 0;
int r[3];
char s[3][MAX_N];
unsigned long long int newton[MAX_N][MAX_N];

void swap(int a, int b) {
  int tmp_r = r[a];
  r[a] = r[b];
  r[b] = tmp_r;
  for(int i = 0; i < n; i++) {
    char tmp = s[a][i];
    s[a][i] = s[b][i];
    s[b][i] = tmp;
  }
}

unsigned long long int area0(int r0) {
  unsigned long long int ret = 0;
  for (int i = 0; i <= r0; i++) {
    ret += newton[n][i];
  }
  return ret;
}

int min(int a, int b) {
  return a < b ? a : b;
}

unsigned long long int area1(int r0, int r1, int s01, int s23) {
  unsigned long long int ret = 0;
  for(int i = 0; i <= r1; i++) {
    for(int j01 = 0; j01 <= min(i, s01); j01++) {
      int j23 = i-j01;
//      if(s23+j-(i-j) <= r0) {
//        continue;
//      }
      if (j23 > s23) {
        continue;
      }
      if (j01 + s23-j23 <= r0) {
        continue;
      }
      ret = (ret + newton[s01][j01] * newton[s23][j23]) % MOD;
    }
  }
  return ret;
}

unsigned long long int area2(int r0, int r1, int r2, int s0, int s1, int s2, int s3) {
  unsigned long long int ret = 0;
  for (int i = r2; i >= 0; i--) { // odleglosc od [2] == i
//    printf("%d         %llu\n", i, ret); fflush(stdout);
    for(int j0 = 0; j0 <= min(i, s0); j0++) {
      int rest0 = i - j0;
      if (rest0 > s1+s3+s2) {
        continue;
      }
      if (j0 + (rest0 > s2 ? s1+s3+s2-(rest0-s2) : s1+s3+rest0) <= r0) {
        continue;
      }
      if (j0 + (rest0 > s3 ? s1+s3+s2-(rest0-s3) : s1+s2+rest0) <= r1) {
        continue;
      }
//      if (j0 + (rest0 > s3+s2 ? rest0 - (s3+s2)))
      for(int j1 = 0; j1 <= min(s1, i-j0); j1++) {
        int rest1 = i - j0 - j1;
        if (rest1 > s3+s2) {
          continue;
        }
        if (j0 + s1-j1 + (rest1 > s2 ? s3+s2-(rest1-s2) : s3+rest0) <= r0) {
          continue;
        }
        if (j0 + s1-j1 + (rest1 > s3 ? s3+s2-(rest1-s3) : s2+rest0) <= r1) {
          continue;
        }

        // for(int j2 = 0; j2 <= min(s2, i-j0-j1); j2++) {
        for(int j2 = min(s2, i-j0-j1); j2 >= 0; j2--) {
          int j3 = i-j0-j1-j2;
          if (j3 > s3) {
            break;
          }
          if (j0 + s1-j1 + s3-j3 + j2 <= r0) {
            break;
          }
          if (j0 + s1-j1 + j3 + s2-j2 <= r1) {
            continue;
          }
          unsigned long long int a = ((newton[s0][j0] * newton[s1][j1]) % MOD);
          unsigned long long int b = ((newton[s2][j2] * newton[s3][j3]) % MOD);
          ret = (ret + (a * b)) % MOD; // TODO: % MOD
        }
      }
    }
  }
  return ret;
}



int main() {
  scanf("%d", &n);
  for(int i = 0; i <= n; i++) {
    newton[0][i] = 0;
  }
  newton[0][0] = 1;
  for(int i = 1; i <= n; i++) {
    newton[i][0] = 1;
    for(int j = 1; j <= n; j++) {
      newton[i][j] = (newton[i-1][j-1] + newton[i-1][j]) % MOD;
    }
  }
//  if (1==1) return 0;

/*  for(int i = 0; i < 20; i++) {
    printf("%llu, ", newton[8][i]);
  }
  printf("\n");*/

  for(int i = 0; i < 3; i++) {
    scanf("%d %s", &(r[i]), &(s[i]));
    max = r[max] > r[i] ? max : i;
  }
  swap(max, 0);
  if (r[2] > r[1]) {
    swap(1, 2);
  }
  for(int i = 0; i < n; i++) {
    if (s[0][i] == s[1][i] && s[1][i] == s[2][i]) { // wszystkie rowne
      t0++;
    } else if (s[0][i] == s[1][i] && s[0][i] != s[2][i]) { // ostatni inny
      t1++;
    } else if (s[0][i] == s[2][i] && s[0][i] != s[1][i]) { // [1] inny
      t2++;
    } else { // [0] inny
      t3++;
    }
  }

  unsigned long long int ret = 0;
  ret = area0(r[0]) % MOD;
  ret = ret + area1(r[0], r[1], t0+t1, t2+t3) % MOD;
  ret = ret + area2(r[0], r[1], r[2], t0, t1, t2, t3) % MOD;
  ret = ret % MOD;

//  printf("t:= %d %d %d %d\n", t0, t1, t2, t3);
//  printf("N = %d\n", n);
/*  for(int i = 0; i < 3; i++) {
    printf("%d %s\n", r[i], s[i]);
  }*/
//  ret = MOD;
  printf("%llu", ret);
  return 0;
}