#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 200005, P = 1000000007;
int n, l[N], r[N], sf[N], sg[N], sh[N];
inline int Pow(ll x, int y=P-2){
int ans=1;
for(; y; y>>=1, x=x*x%P) if(y&1) ans=ans*x%P;
return ans;
}
int main() {
scanf("%d", &n);
for(int i=1; i<=n; ++i) scanf("%d", l+i);
for(int i=1; i<=n; ++i) scanf("%d", r+i);
sh[0]=1;
sh[1]=sg[1]=sf[1]=r[1]-l[1]+1;
for(int i=2; i<=n; ++i){
int len=r[i]-l[i]+1;
sh[i]=(ll)sh[i-1]*len%P;
int len2=max(min(r[i], r[i-1])-max(l[i], l[i-1])+1, 0);
sg[i]=((ll)(sg[i-1]+sh[i-1])*len-(ll)sh[i-2]*len2)%P;
int len3=max(min(min(r[i], r[i-1]), r[i-2])-max(max(l[i], l[i-1]), l[i-2])+1, 0);
sf[i]=((sf[i-1]+2ll*sg[i-1]+sh[i-1])*len-(2ll*sg[i-2]+3ll*sh[i-2])*len2+(i>2?2ll*sh[i-3]:0)*len3)%P;
}
printf("%lld\n", (ll)(sf[n]+P)*Pow(sh[n])%P);
return 0;
}
1389. Create Target Array in the Given Order | 1313. Decompress Run-Length Encoded List |
1281. Subtract the Product and Sum of Digits of an Integer | 1342. Number of Steps to Reduce a Number to Zero |
1528. Shuffle String | 1365. How Many Numbers Are Smaller Than the Current Number |
771. Jewels and Stones | 1512. Number of Good Pairs |
672. Richest Customer Wealth | 1470. Shuffle the Array |
1431. Kids With the Greatest Number of Candies | 1480. Running Sum of 1d Array |
682. Baseball Game | 496. Next Greater Element I |
232. Implement Queue using Stacks | 844. Backspace String Compare |
20. Valid Parentheses | 746. Min Cost Climbing Stairs |
392. Is Subsequence | 70. Climbing Stairs |
53. Maximum Subarray | 1527A. And Then There Were K |
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers | 318. Maximum Product of Word Lengths |
448. Find All Numbers Disappeared in an Array | 1155. Number of Dice Rolls With Target Sum |
415. Add Strings | 22. Generate Parentheses |
13. Roman to Integer | 2. Add Two Numbers |