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
#include<bits/stdc++.h>
using namespace std;
 
ostream& operator<<(ostream &out, string str) {
	for(char c : str) out << c;
	return out;
}
 
template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
	return out << "(" << p.first << ", " << p.second << ")";
}
 
template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
	out << "{";
	for(auto it = a.begin(); it != a.end(); it = next(it))
		out << (it != a.begin() ? ", " : "") << *it;
	return out << "}";
}
 
void dump() { cerr << "\n"; }
template<class T, class... Ts> void dump(T a, Ts... x) {
	cerr << a << ", ";
	dump(x...);
}
 
#ifdef DEBUG
#  define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#  define debug(...) false
#endif
 
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ST first
#define ND second
 
template<class T> int size(T && a) { return a.size(); }
 
using LL = long long;
using PII = pair<int, int>;

template<int mod>
struct mi {
	int val;
	mi() { val = 0; }
	mi(const LL& v) {
		val = (-mod <= v && v <= mod) ? v : v % mod;
		if(val < 0) val += mod;
	}

	friend ostream& operator<<(ostream &os, const mi &a) {
		return os << a.val;
	}
	friend istream& operator>>(istream &is, mi &a) {
		return is >> a.val;
	}

	friend bool operator==(const mi &a, const mi &b) {
		return a.val == b.val;
	}
	friend bool operator!=(const mi &a, const mi &b) {
		return !(a == b);
	}
	friend bool operator<(const mi &a, const mi &b) {
		return a.val < b.val;
	}
	friend bool operator<=(const mi &a, const mi &b) {
		return a.val <= b.val;
	}

	mi operator-() const { return mi(-val); }
	mi& operator+=(const mi &m) {
		if((val += m.val) >= mod) val -= mod;
		return *this;
	}
	mi& operator-=(const mi &m) {
		if((val -= m.val) < 0) val += mod;
		return *this;
	}
	mi& operator*=(const mi &m) {
		val = (LL) val * m.val % mod;
		return *this;
	}
	friend mi qpow(mi a, LL n) {
		if(n == 0) return 1;
		if(n % 2 == 1) return qpow(a, n - 1) * a;
		return qpow(a * a, n / 2);
	}
	friend mi inv(const mi &a) {
		assert(a != 0); return qpow(a, mod - 2);
	}
	mi& operator /=(const mi &m) { 
		return (*this) *= inv(m); 
	}

	friend mi operator+(mi a, const mi &b) { return a += b; }
	friend mi operator-(mi a, const mi &b) { return a -= b; }
	friend mi operator*(mi a, const mi &b) { return a *= b; }
	friend mi operator/(mi a, const mi &b) { return a /= b; }
};
using MI = mi<int(1e9 + 7)>;

struct Newton {
	vector<MI> fac, rev;
	Newton(int n) {
		fac = rev = vector<MI>(n + 1, 1);
		FOR(i, 1, n) fac[i] = fac[i - 1] * i;
		rev[n] = 1 / fac[n];
		for(int i = n; i >= 1; i--)
			rev[i - 1] = rev[i] * i;
	}
	MI get(int n, int k) {
		return fac[n] * rev[n - k] * rev[k];
	}
};

struct hyperball {
	int r;
	bitset<10000> m;
};

int n;
Newton newton(0);

MI intersection(vector<hyperball> balls) {
	assert(size(balls) == 3);

	array<int, 4> cnt = {0, 0, 0, 0};
	for(int i : {1, 2}) balls[i].m ^= balls[0].m;
	REP(i, n) cnt[balls[1].m[i] * 2 + balls[2].m[i]]++;

	MI total = 0;
	vector<MI> cor1(cnt[0] + cnt[1] + 1);
	auto cor2 = cor1;
	vector<vector<MI>> tri1(cnt[0] + 1, vector<MI>(cnt[1] + 1));
	FOR(j, 0, cnt[1]) {
		MI sum = 0;
		MI r = newton.get(cnt[1], j);
		FOR(i, 0, cnt[0]) {
			MI q = r * newton.get(cnt[0], i);
			total += q, sum += q;
			cor1[i + j] += q;
			cor2[i + cnt[1] - j] += q;
			tri1[i][j] = sum;
		}
	}
	auto tri2 = tri1;

	for(int i = size(cor1) - 1; i >= 1; i--) 
		cor1[i - 1] += cor1[i];
	for(int i = size(cor2) - 1; i >= 1; i--) 
		cor2[i - 1] += cor2[i];

	FOR(i, 0, cnt[0]) FOR(j, 0, cnt[1]) {
		if(i && j) tri1[i][j] += tri1[i - 1][j - 1];
		if(i && j != cnt[1]) tri2[i][j] += tri2[i - 1][j + 1];
	}

	MI res = 0;
	FOR(c, 0, min(balls[0].r, cnt[2])) {
		MI _q = newton.get(cnt[2], c);
		FOR(d, 0, min(balls[0].r - c, cnt[3])) {
			MI q = _q * newton.get(cnt[3], d);

			int c1 = min(balls[0].r - c - d, balls[1].r - cnt[2] - cnt[3] + c + d);
			int c2 = balls[2].r - cnt[1] - cnt[3] - c + d;
			if(c1 < 0 || c2 < -cnt[1]) continue;

			int a = c1 + c2;
			if(a % 2 != 0) a--;
			a /= 2;
			int b = a - c2;

			if(a <= cnt[0]) {
				if(a < 0) continue;
				if(b < 0) {
					if(c1 >= 0) res += tri2[c1][0] * q;
					continue;
				}
				if(cnt[1] <= b) {
					b = cnt[1];
					a = c2 + b;
					if(a >= 0) res += tri1[a][b] * q;
					continue;
				}
				res += tri1[a][b] * q;
				b++, a = c1 - b;
				if(a >= 0) res += tri2[a][b] * q;
			}
			else {
				res += total * q;
				if(c1 + 1 < size(cor1)) res -= cor1[c1 + 1] * q;
				if(c2 + cnt[1] + 1 < size(cor2)) res -= cor2[c2 + cnt[1] + 1] * q;
			}
		}
	}

	return res;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	newton = Newton(n);
	auto read = [&]() {
		hyperball b;
		cin >> b.r >> b.m;
		return b;
	};
	vector balls = {read(), read(), read()};

	MI ans = 0;
	REP(i, 3) ans += intersection({balls[i], balls[i], balls[i]});
	REP(i, 3) REP(j, i) ans -= intersection({balls[i], balls[j], balls[j]});
	ans += intersection(balls);

	cout << ans << "\n";
}