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
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

namespace {

using ll = long long;

using Point = vector<char>;
using Sphere = pair<Point, int>;

constexpr int mod = 1'000'000'007;

int mul(int a, int b)
{
  return ll{a} * b % mod;
}

int mul(int a, int b, int c)
{
  return mul(a, mul(b, c));
}

int mul(int a, int b, int c, int d)
{
  return mul(a, mul(b, mul(c, d)));
}

vector<vector<int>> choose;
vector<vector<int>> pchoose;

void init_choose(int d)
{
  choose.resize(d + 1);
  pchoose.resize(d + 1);
  for (int n = 0; n <= d; ++n) {
    choose[n].resize(n + 1);
    choose[n][0] = choose[n][n] = 1;
    for (int k = 1; k < n; ++k) {
      choose[n][k] = (choose[n - 1][k] + choose[n - 1][k - 1]) % mod;
    }
    pchoose[n].resize(n + 1);
    pchoose[n][0] = 1;
    for (int k = 1; k <= n; ++k) {
      pchoose[n][k] = (pchoose[n][k - 1] + choose[n][k]) % mod;
    }
  }
}

int distance(Point const& p, Point const& q)
{
  int d = p.size();
  int res = 0;
  for (int i = 0; i < d; ++i) {
    if (p[i] != q[i]) ++res;
  }
  return res;
}

int solve(Sphere const& s)
{
  int d = s.first.size();
  int r = s.second;
  return pchoose[d][r];
}

int solve(Sphere const& s1, Sphere const& s2)
{
  auto const& [o1, r1] = s1;
  auto const& [o2, r2] = s2;
  int d = o1.size();
  int b = distance(o1, o2);
  int a = d - b;
  if (b > r1 + r2) return 0;
  int res = 0;
  for (int y = 0; y <= b; ++y) {
    int x = min({r1 - y, r2 - (b - y), a});
    if (x < 0) continue;
    res += mul(pchoose[a][x], choose[b][y]);
    res %= mod;
  }
  return res;
}

int solve(Sphere const& s1, Sphere const& s2, Sphere const& s3)
{
  auto const& [o1, r1] = s1;
  auto const& [o2, r2] = s2;
  auto const& [o3, r3] = s3;
  int d = o1.size();
  if (distance(o1, o2) > r1 + r2) return 0;
  if (distance(o1, o3) > r1 + r3) return 0;
  if (distance(o2, o3) > r2 + r3) return 0;
  int cnt[2][2] = {};
  for (int i = 0; i < d; ++i) {
    int a1 = o1[i];
    int a2 = o2[i];
    int a3 = o3[i];
    ++cnt[a2 ^ a1][a3 ^ a1];
  }
  int res = 0;
  for (int x = 0; x <= cnt[0][1]; ++x) {
    if (x > r1) break;
    for (int y = 0; y <= cnt[1][0]; ++y) {
      if (x + y > r1) break;
      for (int z = 0; z <= cnt[1][1]; ++z) {
        int d1 = x + y + z;
        if (d1 > r1) break;
        int d2 = x + cnt[1][0] - y + cnt[1][1] - z;
        int d3 = cnt[0][1] - x + y + cnt[1][1] - z;
        int q = min({cnt[0][0], r1 - d1, r2 - d2, r3 - d3});
        if (q < 0) continue;
        res += mul(pchoose[cnt[0][0]][q], choose[cnt[0][1]][x], choose[cnt[1][0]][y], choose[cnt[1][1]][z]);
        res %= mod;
      }
    }
  }
  return res;
}

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int d;
  cin >> d;

  init_choose(d);

  vector<Sphere> spheres(3);
  for (auto& s: spheres) {
    cin >> s.second;
    s.first.resize(d);
    for (auto& x: s.first) {
      char c;
      cin >> c;
      x = c - '0';
    }
  }

  {
    auto cmp = [](Sphere const& lhs, Sphere const& rhs) {
      if (lhs.first != rhs.first) return lhs.first < rhs.first;
      return lhs.second > rhs.second;
    };
    sort(spheres.begin(), spheres.end(), cmp);
  }
  {
    auto cmp = [](Sphere const& lhs, Sphere const& rhs) {
      return lhs.first == rhs.first;
    };
    spheres.erase(unique(spheres.begin(), spheres.end(), cmp), spheres.end());
  }
  {
    auto cmp = [](Sphere const& lhs, Sphere const& rhs) {
      return lhs.second < rhs.second;
    };
    sort(spheres.begin(), spheres.end(), cmp);
  }

  int n = spheres.size();

  int res = 0;

  for (int i = 0; i < n; ++i) {
    res += solve(spheres[i]);
    res %= mod;
    for (int j = i + 1; j < n; ++j) {
      res += mod;
      res -= solve(spheres[i], spheres[j]);
      res %= mod;
      for (int k = j + 1; k < n; ++k) {
        res += solve(spheres[i], spheres[j], spheres[k]);
        res %= mod;
      }
    }
  }

  cout << res << endl;

  return 0;
}