#include <algorithm>
#include <iterator>
#ifdef LOCAL
#include "dependencies.h"
#else
#include <bits/stdc++.h>
#endif
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) \
do { \
} while (false)
#endif
template <typename T> bool chkmin(T &x, const T &y) {
return x > y ? x = y, true : false;
}
template <typename T> bool chkmax(T &x, const T &y) {
return x < y ? x = y, true : false;
}
#ifdef ATCODER
#include "atcoder/all"
#endif
template <unsigned int P> struct Fp {
unsigned int v = 0;
// reflection
template <typename T = int> static constexpr T mod() { return P; }
template <typename T = int> constexpr T val() { return v; }
// constructor
constexpr Fp() = default;
template <typename T> constexpr Fp(T x) : v(x % mod()) {
if constexpr (std::is_signed_v<T>) {
if (v >> 31) {
v += P;
}
}
}
// io
friend std::istream &operator>>(std::istream &is, Fp &rhs) {
long long v;
is >> v;
rhs = v;
return is;
}
friend std::ostream &operator<<(std::ostream &os, Fp rhs) {
return os << rhs.v;
}
// comparision
friend constexpr bool operator==(Fp lhs, Fp rhs) { return lhs.v == rhs.v; }
friend constexpr bool operator!=(Fp lhs, Fp rhs) { return lhs.v != rhs.v; }
// arithmetic
constexpr Fp &operator+=(Fp rhs) {
v += rhs.v;
if (v >= P)
v -= P;
return *this;
}
constexpr Fp &operator-=(Fp rhs) {
v -= rhs.v;
if (v >> 31)
v += P;
return *this;
}
constexpr Fp &operator*=(Fp rhs) {
v = static_cast<unsigned long long>(v) * rhs.v % P;
return *this;
}
constexpr Fp &operator/=(Fp rhs) {
v = fpow(rhs.v, P - 2, v);
return *this;
}
template <typename T> constexpr Fp &operator^=(T rhs) {
v = fpow(v, rhs);
return *this;
}
friend constexpr Fp operator+(Fp lhs, Fp rhs) { return lhs += rhs; }
friend constexpr Fp operator-(Fp lhs, Fp rhs) { return lhs -= rhs; }
friend constexpr Fp operator*(Fp lhs, Fp rhs) { return lhs *= rhs; }
friend constexpr Fp operator/(Fp lhs, Fp rhs) { return lhs /= rhs; }
template <typename T> friend constexpr Fp operator^(Fp lhs, T rhs) {
return lhs ^= rhs;
}
constexpr Fp operator+() const { return *this; }
constexpr Fp operator-() const { return Fp{} - *this; }
constexpr Fp operator~() const { return fpow(v, P - 2); }
template <typename T> constexpr Fp pow(T exp) const { return fpow(v, exp); }
// x^y * z
template <typename T>
static constexpr unsigned int fpow(unsigned long long x, T y,
unsigned long long z = 1) {
unsigned int n = y % (mod() - 1);
if constexpr (std::is_signed_v<T>) {
if (n >> 31) {
n += P - 1;
}
}
for (; n; n /= 2) {
if (n & 1)
z = z * x % P;
x = x * x % P;
}
return z;
}
};
using Z353 = Fp<998244353>;
using Z007 = Fp<1000000007>;
using Z009 = Fp<1000000009>;
void initialize() {
std::cin.tie(nullptr)->sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(10);
}
template <typename T>
using Heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <typename T> void unique(std::vector<T> &a) {
std::sort(a.begin(), a.end());
a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <typename T> bool contains(const std::vector<T> &a, const T &x) {
auto iter = std::lower_bound(a.begin(), a.end(), x);
return iter != a.end() && *iter == x;
}
void solution(int cas);
int main() {
initialize();
int T = 1;
std::cin >> T;
for (int cas = 1; cas <= T; ++cas) {
solution(cas);
}
}
void solution([[maybe_unused]] int cas) {
// TODO
using Z = Z007;
int n;
std::cin >> n;
std::vector<int> l(n), r(n);
for (int i = 0; i < n; ++i) {
std::cin >> l[i];
}
for (int i = 0; i < n; ++i) {
std::cin >> r[i];
++r[i];
}
auto DP = [&](int x) -> Z {
std::array<Z, 3> dp = {};
dp[0] = 1;
for (int i = 0; i < n; ++i) {
std::array<Z, 3> np = {};
int a = std::max(0, r[i] - std::max(l[i], x)), b = r[i] - l[i] - a;
np[0] = (dp[0] + dp[1]) * b;
np[1] = dp[0] * a;
np[2] = dp[1] * a + dp[2] * (a + b);
dp.swap(np);
}
debug(x, dp[2]);
return dp[2];
};
std::vector<int> key = l;
std::copy(r.begin(), r.end(), std::back_inserter(key));
unique(key);
Z ans = 0;
constexpr int N = 50000;
for (int i = 1; i < (int)key.size(); ++i) {
int L = key[i - 1], R = key[i];
if (R - L <= N) {
for (int x = L; x < R; ++x) {
ans += DP(x);
}
}
else {
std::vector<Z> y(N + 1);
for (int x = L; x <= L + N; ++x) {
y[x - L] = DP(x);
}
CE fuck xrx
}
}
std::cout << ans << '\n';
}