Cow and Treats
Contest: Codeforces Round 621 (Div. 1 + Div. 2)
Difficulty rating: 2500
Tags: binary search, combinatorics, dp, greedy, implementation, math

After a successful year of milk production, Farmer John is rewarding his cows with their favorite treat: tasty grass!

On the field, there is a row of $$$n$$$ units of grass, each with a sweetness $$$s_i$$$. Farmer John has $$$m$$$ cows, each with a favorite sweetness $$$f_i$$$ and a hunger value $$$h_i$$$. He would like to pick two disjoint subsets of cows to line up on the left and right side of the grass row. There is no restriction on how many cows must be on either side. The cows will be treated in the following manner:

- The cows from the left and right side will take turns feeding in an order decided by Farmer John.
- When a cow feeds, it walks towards the other end without changing direction and eats grass of its favorite sweetness until it eats $$$h_i$$$ units.
- The moment a cow eats $$$h_i$$$ units, it will fall asleep there, preventing further cows from passing it from both directions.
- If it encounters another sleeping cow or reaches the end of the grass row, it will get upset. Farmer John absolutely does not want any cows to get upset.

Note that grass does not grow back. Also, to prevent cows from getting upset, not every cow has to feed since FJ can choose a subset of them.

Surprisingly, FJ has determined that sleeping cows are the most satisfied. If FJ orders optimally, what is the maximum number of sleeping cows that can result, and how many ways can FJ choose the subset of cows on the left and right side to achieve that maximum number of sleeping cows (modulo $$$10^9+7$$$)? The order in which FJ sends the cows does not matter as long as no cows get upset.

Input format:
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 5000$$$, $$$1 \le m \le 5000$$$)  — the number of units of grass and the number of cows.

The second line contains $$$n$$$ integers $$$s_1, s_2, \ldots, s_n$$$ ($$$1 \le s_i \le n$$$)  — the sweetness values of the grass.

The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$f_i$$$ and $$$h_i$$$ ($$$1 \le f_i, h_i \le n$$$)  — the favorite sweetness and hunger value of the $$$i$$$-th cow. No two cows have the same hunger and favorite sweetness simultaneously.

Output format:
Output two integers  — the maximum number of sleeping cows that can result and the number of ways modulo $$$10^9+7$$$.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example, FJ can line up the cows as follows to achieve $$$2$$$ sleeping cows:

- Cow $$$1$$$ is lined up on the left side and cow $$$2$$$ is lined up on the right side.
- Cow $$$2$$$ is lined up on the left side and cow $$$1$$$ is lined up on the right side.

In the second example, FJ can line up the cows as follows to achieve $$$1$$$ sleeping cow:

- Cow $$$1$$$ is lined up on the left side.
- Cow $$$2$$$ is lined up on the left side.
- Cow $$$1$$$ is lined up on the right side.
- Cow $$$2$$$ is lined up on the right side.

In the third example, FJ can line up the cows as follows to achieve $$$2$$$ sleeping cows:

- Cow $$$1$$$ and $$$2$$$ are lined up on the left side.
- Cow $$$1$$$ and $$$2$$$ are lined up on the right side.
- Cow $$$1$$$ is lined up on the left side and cow $$$2$$$ is lined up on the right side.
- Cow $$$1$$$ is lined up on the right side and cow $$$2$$$ is lined up on the left side.

In the fourth example, FJ cannot end up with any sleeping cows, so there will be no cows lined up on either side.

Example input:
5 2
1 1 1 1 1
1 2
1 3

Example output:
2 2

Example input:
5 2
1 1 1 1 1
1 2
1 4

Example output:
1 4

Editorial:
First, we observe that it is impossible to send more than one cow with the same favorite sweetness on the same side without upsetting any of them. This means we can send at most two cows of each favorite sweetness, one on each side.
Now, let's assume we know the index of the rightmost cow that came from the left side.
For each sweetness $$$i$$$, we denote the number of units of grass to the left of the index as $$$l_i$$$ and to the right as $$$r_i$$$. There are three cases we have to consider. If there does not exist a cow of this favorite sweetness or the one of this favorite sweetness with minimum hunger cannot be satisfied from either direction, then $$$0$$$ cows of the type will be asleep. Otherwise, $$$1$$$ or $$$2$$$ cows will be asleep, and we can derive a simple formula based on $$$l_i$$$, $$$r_i$$$, and the cows of this type. Remember that we always maximize the number of sleeping cows first.
We maintain how much each sweetness contributed to the answer. When we shift this index of the rightmost cow to the right, we can undo and recompute our answer.
You can speed up the solution using binary search, but we chose not to require it. We also chose to allow other $$$O(n^2)$$$ and $$$O(n^2\log{n})$$$ solutions to pass.
Time Complexity: $$$O(n\log{n})$$$