概率dp
#include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <numeric>
#define ll long long
#define PII pair<int, int>
#define TUP tuple<ll, ll, ll>
using namespace std;
const int N = 60;
const int mod = 1e9 + 7;
int ta[3], tb[3];
ll dp[60][60];//小红还有i张卡片1,小紫还有j张卡片1
ll inv(ll a) {
ll ret = 1, b = mod - 2;
while (b) {
if (b % 2) ret = ret * a % mod;
b >>= 1;
a = a * a % mod;
}
return ret;
}
int main() {
int n, m, x;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> x, ta[x]++;
for (int i = 0; i < m; i++) cin >> x, tb[x]++;
//dp[i][j] = p1 * dp[i - 1][j] + p2 * dp[i][j - 1];
//dp[i][j] /= p1 + p2;
dp[0][0] = 0;
if (!ta[2] && !tb[2]) {
cout << 0;
return 0;
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
ll p1 = i * inv(ta[2] + i) % mod * tb[2] % mod * inv(tb[2] + j) % mod;
ll p2 = ta[2] * inv(ta[2] + i) % mod * j % mod * inv(tb[2] + j) % mod;
dp[i][j] = dp[i - 1][j] * p1 % mod + p2 * dp[i][j - 1] % mod + 1;
dp[i][j] *= inv(p1 + p2);
dp[i][j] %= mod;
}
}
cout << dp[ta[1]][tb[1]];
return 0;
}