QOJ.ac

QOJ

Limite de temps : 2 s Limite de mémoire : 256 MB Points totaux : 100

#10503. Reachability

Statistiques

Byteotia is an island located in the ocean, exceptionally rich in valuable natural resources. The island's area is square, and its four sides face the four cardinal directions. Ports have been built on the western and southern shores of Byteotia. Until now, there has been no road infrastructure on the island connecting the ports. However, unfavorable ocean currents make sailing from the ports on the western shore to the ports on the southern shore (and consequently, the transport of raw materials) cumbersome and very expensive. For this reason, it was decided to build a network of roads and intersections on the territory of Byteotia, which would (partially) solve the problem of transport from the western shore to the southern shore. Unfortunately, it is already known that there is not enough money to build any underground tunnels, viaducts, or overpasses.

For simplicity, let us assume that the area of Byteotia is a square with opposite corners at points $(0, 0)$ and $(10^9, 10^9)$ in a Cartesian coordinate system. Each of the $n$ ports on the western shore is located at a certain point on the $OY$ axis, while each of the $m$ ports on the southern shore is located at a certain point on the $OX$ axis. The locations of the ports are pairwise distinct.

The plan for building the road infrastructure assumes the construction of a certain (finite) number of intersections and a certain number of one-way roads connecting intersections and/or ports. The roads and intersections together form a road network. We assume that intersections and ports must be placed at pairwise distinct points on the island, and the roads correspond to arbitrary non-self-intersecting curves, completely contained within the island's area, with their beginnings and ends at certain points where intersections or ports are located. The only common points of two roads can be their beginnings or ends.

The figure below shows three example road networks for $n = m = 2$. The gray area represents Byteotia, while black squares represent ports, and black circles represent intersections.

Of course, there are infinitely many possible road networks. We call two networks $A$ and $B$ equivalent if, for every port $x$ on the western shore and every port $y$ on the southern shore, network $A$ allows travel by road from port $x$ to port $y$ if and only if network $B$ allows travel by road from $x$ to $y$. In the example above, the networks in the middle and right drawings are equivalent.

Given the locations of the ports on the western and southern shores of Byteotia, find the size of the largest set of pairwise non-equivalent road networks satisfying the stated assumptions.

Input

The first line of input contains two integers $n, m$ ($1 \le n, m \le 500$) denoting the number of ports on the western shore and the number of ports on the southern shore, respectively. The second line contains $n$ pairwise distinct integers $y_1, \dots, y_n$ ($1 \le y_i \le 10^9$), describing the locations of the ports on the western shore: the $i$-th of these ports lies at point $(0, y_i)$. The third line contains $m$ pairwise distinct integers $x_1, \dots, x_m$ ($1 \le x_j \le 10^9$), describing the locations of the ports on the southern shore: the $j$-th of these ports lies at point $(x_j, 0)$.

Output

Output the size of the largest set of pairwise non-equivalent road networks, modulo $10^9 + 7$.

Examples

Input 1

2 2
1 2
1 2

Output 1

13

Input 2

8 9
39 58 64 23 72 66 80 30
93 23 33 72 79 48 19 92 98

Output 2

914854829

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.