"This was made by Thror, your grandfather, Thorin", he said in answer to the dwarves' excited questions. "It is a plan of the Mountain."
J. R. R. Tolkien, The Hobbit, or There and Back Again
The plan of the Lonely Mountain is two parallel projections of the mountain onto two projection planes. Both planes are perpendicular to the ground and to each other. Each projection has a mountain-like shape.
Bilbo has never seen the mountain, so he tries to picture it. Is a single mountain really standing there alone, or do ridges and other mountains surround it? Either way it has to be enormous, since it holds the whole kingdom of the dwarves.
Bilbo decided to estimate the largest volume the Lonely Mountain and the mountains around it can occupy, working only from the plan Gandalf gave him.
Put the ground on the plane z=0. The first projection is drawn on the Oxz plane and the second one on the Oyz plane. A projection given by the points (x1,z1),…,(xn,zn) means the region {(x,z):x1≤x≤xn, 0≤z≤f(x)}, where f is the piecewise linear function running through those points in order.
Among all solids whose parallel projection onto Oxz is exactly the first region and whose parallel projection onto Oyz is exactly the second region, find the volume of the largest one.
The first line contains one integer nx, the number of points in the parallel projection of the mountain onto the plane Oxz (2≤nx≤100000). The second line contains nx pairs of integers xi,zi, the coordinates of the polygonal chain that forms this projection (−109≤x1<x2<⋯<xnx≤109, 0≤zi≤109, z1=znx=0).
The next two lines contain the projection onto the Oyz plane in the same format.
If no solid matches both projections, print Invalid plan.
Otherwise the largest possible volume is a rational number. Write it as an irreducible fraction p/q. Under the constraints above q is never a multiple of 1000000007, so exactly one integer r satisfies r×q≡p(mod1000000007) and 0≤r<1000000007. Print that r.