#P5979. [PA 2014] Druzyny

[PA 2014] Druzyny

Description

In a PE class, nn children stand in a line (numbered from 11 to nn). The teacher wants to divide them into several groups. Each group must contain a consecutive segment of children, and each child belongs to exactly one group.

The ii-th child wants the size of their group to be no more than did_i and no less than cic_i, otherwise they will be unhappy.

Assuming all children are satisfied, find the maximum possible number of groups, and how many grouping plans can achieve this maximum.

Input Format

The first line contains an integer nn, which is the number of children.

The next nn lines each contain two integers ci,dic_i, d_i, representing the minimum and maximum allowed group size for the group containing child ii.

Output Format

If no such plan exists, output only one line: NIE.

Otherwise, output one line containing two integers: the maximum number of groups and the number of plans. (The number of plans should be taken modulo 109+710^9+7.)

9
1 4
2 5
3 4
1 5
1 1
2 5
3 5
1 3
1 1
5 2
2
1 1
2 2
NIE

Hint

For 100%100\% of the testdata, 1n1061 \le n \le 10^6, and 1cidin1 \le c_i \le d_i \le n.

Translated by ChatGPT 5