奉上 C++ 代码
#include <bits/stdc++.h>
using i64 = int64_t;
using u64 = uint64_t;
using f64 = double_t;
using i128 = __int128_t;
template<class T>
constexpr T power(T a, i64 b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
template<int P>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(i64 x) : x{norm(x % getMod())} {}
static int Mod;
constexpr static int getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(int Mod_) {
Mod = Mod_;
}
constexpr int norm(int x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr int val() const {
return x;
}
explicit constexpr operator int() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) & {
x = 1LL * x * rhs.x % getMod();
return *this;
}
constexpr MInt &operator+=(MInt rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) & {
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
i64 v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
};
template<>
int MInt<0>::Mod = 998244353;
template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 1000000007;
using Z = MInt<P>;
// 设 E[i] 表示从 i 开始到 n 的期望
// 经整理是三对角矩阵,所以可以只记录对角线上的系数,以及右侧的(增广)
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(12);
int n;
std::cin >> n;
std::vector<int> a(n - 1);
for (int i = 0; i < n - 1; i++) {
std::cin >> a[i];
}
std::vector<int> b(n - 1);
for (int i = 0; i < n - 1; i++) {
std::cin >> b[i];
}
std::vector<Z> x(n - 1);
std::vector<Z> y(n - 1);
std::vector<Z> z(n - 1);
x[0] = Z(a[0]) / (a[0] + b[0]);
y[0] = Z(b[0]) / (a[0] + b[0]) - 1;
for (int i = 1; i < n - 1; i++) {
Z dn = Z(a[i] + b[i]) * (a[i] + b[i]);
x[i] = Z(a[i]) * a[i] / dn;
y[i] = 2 * Z(a[i]) * b[i] / dn - 1;
z[i] = Z(b[i]) * b[i] / dn;
}
std::vector<Z> s(n - 1, -1);
for (int i = 0; i < n - 1; i++) {
s[i] /= y[i];
x[i] /= y[i];
if (i + 1 < n - 1) {
s[i + 1] -= z[i + 1] * s[i];
y[i + 1] -= z[i + 1] * x[i];
}
}
for (int i = n - 3; i >= 0; i--) {
s[i] -= s[i + 1] * x[i];
}
std::cout << s[0] + 1 << "\n";
return 0;
}