This page is still under construction.

Parts of this page are still being built. What you see may change.

Teams

Time limit5sMemory limit256 MB

Summary
Split the row into the most contiguous teams so each student's team size lies within their given range, and count those optimal splits.
Level

Medium7 of 10

Topics
Dynamic programming, Segment tree, Intervals
Solved
No attempts yet

Problem

Mr. Bajtocki is the most popular physical education teacher at Travelling Salesman Bajtazar Primary School No. 64 in Byteotia. In every lesson, after a short warm up, he asks the students which team game they want to play and then helps them split into teams.

At the roll call the students stand in one row and take the numbers 11 to nn in that order. Mr. Bajtocki forms the teams so that every team is a contiguous block of the row. Each student belongs to exactly one team.

The teacher knows his students well, so he knows that student ii is happy with the split only if the number of players in that student's team is at least cic_i and at most did_i.

Decide whether the students can be split so that every student is happy. If they can, find the maximum possible number of teams and the number of splits that reach this maximum.

Input

The first line contains one integer nn (1≤n≤1061 \le n \le 10^6), the number of students.

Each of the next nn lines describes one student. The ii-th of these lines contains two integers cic_i and did_i (1≤ci≤di≤n1 \le c_i \le d_i \le n). Student ii is happy when the size of that student's team lies in the range [ci,di][c_i, d_i].

Output

If the students can be split so that every student is happy, print two integers separated by a single space. The first is the maximum number of teams, the second is the number of splits that reach this maximum, taken modulo 109+710^9+7.

If no such split exists, print NIE.

Examples3

  1. Example 1

    Input
    9
    1 4
    2 5
    3 4
    1 5
    1 1
    2 5
    3 5
    1 3
    1 1
    
    Expected output
    5 2
    
  2. Example 2

    Input
    2
    1 1
    2 2
    
    Expected output
    NIE
    
  3. Example 3

    Input
    1
    1 1
    
    Expected output
    1 1