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
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

struct Sphere {
  int radius;
  std::string coordinates;
};

struct Update {
  int from;
  int to;
  int amount;
};

int EditDistance(std::string lhs, std::string rhs) {
  int diffs = 0;
  for (int i = 0; i < lhs.size(); i++) {
    if (lhs[i] != rhs[i]) {
      diffs++;
    }
  }
  return diffs;
}

std::vector<int> EditDistance3(std::string c1, std::string c2, std::string c3) {
  std::vector<int> result;
  result.emplace_back(0);
  result.emplace_back(0);
  result.emplace_back(0);
  for (int i = 0; i < c1.size(); i++) {
    if (c1[i] == c2[i]) {
      if (c1[i] != c3[i]) {
        result[2]++; // c3 unique
      }
    } else {
      if (c1[i] == c3[i]) {
        result[1]++; // c2 unique
      } else {
        result[0]++; // c1 unique
      }
    }
  }
  return result;
}

int min3(int a, int b, int c) {
  return std::min(a, std::min(b, c));
}

constexpr int mod = 1e9 + 7;

int main(int argc, char** argv) {
  int dims;
  std::cin >> dims;
  Sphere* spheres = new Sphere[3];
  for (int i = 0; i < 3; i++) {
    std::cin >> spheres[i].radius;
    std::cin >> spheres[i].coordinates;
  }

  std::vector<std::vector<int>> binoms;
  std::vector<std::vector<int>> binom_sums;
  for (int i = 0; i <= dims; i++) {
    binoms.emplace_back();
    binom_sums.emplace_back();
    int binom_sum = 0;
    for (int j = 0; j <= i; j++) {
      if (i == 0) {
        binoms[i].emplace_back(1);
        binom_sums[i].emplace_back(1);
      } else {
        int binom = 0;
        if (j > 0) binom += binoms[i - 1][j - 1];
        if (j < i) binom += binoms[i - 1][j];
        binoms[i].emplace_back(binom % mod);

        binom_sum += binom % mod;
        binom_sum %= mod;
        binom_sums[i].emplace_back(binom_sum);
      }
    }
  }

  int result = 0;

  // Single spheres
  for (int i = 0; i < 3; i++) {
    result += binom_sums[dims][spheres[i].radius];
    result %= mod;
  }

  // Common area of two spheres
  for (int i = 0; i < 3; i++) {
    for (int j = i + 1; j < 3; j++) {
      const Sphere& lhs = spheres[i];
      const Sphere& rhs = spheres[j];
      int edit_distance = EditDistance(lhs.coordinates, rhs.coordinates);
      for (int e = 0; e <= edit_distance; e++) {
        if (lhs.radius - e < 0 || rhs.radius - edit_distance + e < 0) {
          continue;
        }
        int min_rad = std::min(lhs.radius - e, rhs.radius - edit_distance + e);
        min_rad = std::min(min_rad, dims - edit_distance);
        long long product =
            ((long long) binom_sums[dims - edit_distance][min_rad])
                * ((long long) binoms[edit_distance][e]) % ((long long) mod);
        result += mod - ((int) product);
        result %= mod;
      }
    }
  }

  // Common area of three spheres
  const Sphere& s0 = spheres[0];
  const Sphere& s1 = spheres[1];
  const Sphere& s2 = spheres[2];
  auto
      distances = EditDistance3(s0.coordinates, s1.coordinates, s2.coordinates);
  int remainder = dims - distances[0] - distances[1] - distances[2];

  std::unordered_map<int, int> r2_radius_to_count = {};
  std::unordered_map<int, int> r01_radius_to_count = {};
  std::unordered_map<int, std::vector<Update>> d2_to_update = {};

  for (int d0 = 0; d0 <= distances[0]; d0++) {
    long long mult0 = ((long long) binoms[distances[0]][d0]);
    for (int d1 = 0; d1 <= distances[1]; d1++) {
      int count = (int) (mult0 * binoms[distances[1]][d1] % mod);
      int rad0 = s0.radius - d0 + d1 - distances[1] - distances[2];
      int rad1 = s1.radius - d1 + d0 - distances[0] - distances[2];
      int rad2 = s2.radius + d0 + d1 - distances[0] - distances[1];
      int min_rad = std::min(rad0, rad1);

      if (rad2 < min_rad) {
        r2_radius_to_count[rad2] = (r2_radius_to_count[rad2] + count) % mod;
      } else {
        r01_radius_to_count[min_rad] =
            (r01_radius_to_count[min_rad] + count) % mod;

        int steps = (rad2 - min_rad + 1) / 2;

        if (steps <= distances[2]) {
          Update update;
          update.amount = count;
          update.from = min_rad;
          update.to = rad2;
          d2_to_update[steps].push_back(update);
        }
      }
    }
  }

  for (int d2 = 0; d2 <= distances[2]; d2++) {
    for (const auto& update : d2_to_update[d2]) {
      r01_radius_to_count[update.from] =
          (r01_radius_to_count[update.from] + mod - update.amount) % mod;
      r2_radius_to_count[update.to] =
          (r2_radius_to_count[update.to] + update.amount) % mod;
    }

    for (const auto& entry : r2_radius_to_count) {
      int radius = entry.first;
      int actual_radius = std::min(remainder, radius - d2);
      if (actual_radius < 0) {
        continue;
      }
      int count = entry.second;
      long long add = ((long long) count) * binoms[distances[2]][d2] % mod
          * binom_sums[remainder][actual_radius] % mod;
      result = (result + add) % mod;
    }

    for (const auto& entry : r01_radius_to_count) {
      int radius = entry.first;
      int actual_radius = std::min(remainder, radius + d2);
      if (actual_radius < 0) {
        continue;
      }
      int count = entry.second;
      long long add = ((long long) count) * binoms[distances[2]][d2] % mod
          * binom_sums[remainder][actual_radius] % mod;
      result = (result + add) % mod;
    }
  }

  std::cout << result << std::endl;
}