1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <iostream>
#include <cassert>
using namespace std;
#define REP(i,n) for(int _n=n, i=0;i<_n;++i)
#define FOR(i,a,b) for(int _b=(b), i=(a);i<=_b;++i)

class Input {
 public:
  Input() { bufpos = bufend = buffer; eof = false; }
  bool Eof() { return eof; }
  char Peek() { if(bufpos == bufend) Grab(); return *bufpos; }
  unsigned char UPeek() { return static_cast<unsigned char>(Peek()); }
  void SkipWS();
  template<class T> T Get();
  void operator()() {}
  template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) {
    arg = Get<Arg>();
    operator()(args...);
  }
 private:
  static const int BUFSIZE = 1<<16;
  char buffer[BUFSIZE];
  char *bufpos;
  char *bufend;
  bool eof;
  void Grab();
};

void Input::Grab() {
  if(eof) return;
  bufpos = buffer;
  bufend = buffer + read(0, buffer, BUFSIZE);
  if(bufend==bufpos) { eof=true; *bufpos=0; }
}

template<> inline char Input::Get<char>() {
  char res = Peek();
  ++bufpos;
  return res;
}

void Input::SkipWS() {
  while(isspace(UPeek())) Get<char>();
}

template<> unsigned Input::Get<unsigned>() {
  SkipWS();
  unsigned x = 0;
  while(isdigit(UPeek())) {
    x = 10u * x + (Get<char>()-'0');
  }
  return x;
}

template<> int Input::Get<int>() {
  SkipWS();
  bool neg = false;
  if(Peek()=='-') { neg=true; Get<char>(); }
  unsigned x = Get<unsigned>();
  if (neg) x = -x;
  return static_cast<int>(x);
}

Input IN;

const unsigned MOD = 1000000007;

class Mod {
  unsigned v;
 public:
  Mod() {}
  Mod(int x):v(x%MOD) {}
  int get() const { return v; }
  Mod operator+(Mod b) const { return Mod(v+b.v); }
  void operator+=(Mod b) { *this = *this + b; }
  Mod operator-(Mod b) const { return Mod(v+MOD-b.v); }
  void operator-=(Mod b) { *this = *this - b; }
};

const int MAXN = 1000005;
const int IMPOSSIBLE = -1;

struct Linked {
  Linked *prev;
  Linked *next;
  void SetEmpty() { prev = this; next = this; }
  bool Empty() { return next == this; }
  void Extract() {
    if(Empty()) return;
    prev->next = next;
    next->prev = prev;
    SetEmpty();
  }
  void InsertBefore(Linked *what) {
    what->prev = prev;
    what->next = this;
    prev->next = what;
    prev = what;
  }
  void InsertAfter(Linked *what) {
    next->InsertBefore(what);
  }
  void SpliceBefore(Linked *head) {
    if(head->Empty()) return;
    prev->next = head->next;
    head->next->prev = prev;
    prev = head->prev;
    head->prev->next = this;
    head->SetEmpty();
  }
} __attribute__((aligned(8)));

#define CONTAINER(ptr, type, member) (reinterpret_cast<type *>(reinterpret_cast<char*>(ptr) - offsetof(type,member)))

struct Result {
  int maxSteps;  // or IMPOSSIBLE
  Mod poss;
};

struct Vertex {
  Linked inLengthStartOrActive;
  Linked inLengthEnd;
  Result result;
  int active; // 1,0,-1
} __attribute__((aligned(32)));

struct Length {
  Linked headStart;  // list of Vertex, sorted
  Linked headActive; // list of Vertex, sorted
  Linked headEnd;  // list of Vertex, sorted
  Linked inHereStart;
  Linked inHereEnd;
  Linked inLengthsStartOrActive;
  Linked inLengthsEnd;
};

struct Here {
  Linked headLengthsStart;  // list of Length
  Linked headLengthsEnd;    // list of Length
} __attribute__((aligned(16)));

int N;
Vertex gVertices[MAXN+1];    // 32MB
Length gLengths[MAXN+1];   // 56MB
Here gHere[MAXN+1];  // 16MB
Linked gHeadLengthsStartOrActive;  // list of Length sorted by len
Linked gHeadLengthsEnd;

const int TREE_SIZE = 1<<20;
int gStepsTree[TREE_SIZE*2]; // 8 MB, tree of steps->nactive
Mod gPoss[MAXN+1]; // 4 MB, steps->poss

struct StaticAssertions {
  static_assert(sizeof(Linked)==8, "sizeof Linked");
  static_assert(sizeof(Vertex)==32, "sizeof Vertex");
  static_assert(sizeof(Length)==56, "sizeof Length");
  static_assert(sizeof(Here)==16, "sizeof Here");
  static_assert(
      sizeof(gVertices) +
      sizeof(gLengths) +
      sizeof(gHere) +
      sizeof(gStepsTree) +
      sizeof(gPoss) <
      (116 << 20),
      "memory use");
};

void Initialize() {
  ios_base::sync_with_stdio(false); cin.tie(nullptr);
  IN(N);
  FOR(x,0,N) {
    Vertex *v = &gVertices[x];
    v->inLengthStartOrActive.SetEmpty();
    v->inLengthEnd.SetEmpty();
    v->result.maxSteps = IMPOSSIBLE;
    v->result.poss = 0;
    v->active = 0;
  }
  gVertices[0].result.maxSteps = 0;
  gVertices[0].result.poss = 1;
  FOR(len,0,N) {
    Length *L = &gLengths[len];
    L->headStart.SetEmpty();
    L->headActive.SetEmpty();
    L->headEnd.SetEmpty();
    L->inHereStart.SetEmpty();
    L->inHereEnd.SetEmpty();
    L->inLengthsStartOrActive.SetEmpty();
    L->inLengthsEnd.SetEmpty();
  }
  FOR(x,0,N) {
    Here *h = &gHere[x];
    h->headLengthsStart.SetEmpty();
    h->headLengthsEnd.SetEmpty();
  }
  gHeadLengthsStartOrActive.SetEmpty();
  gHeadLengthsEnd.SetEmpty();

  memset(gStepsTree, 0, sizeof(gStepsTree));
  FOR(i,0,N) gPoss[i] = 0;
}

void Activate(Vertex *v) {
  if(v->active != 0) return;
  v->active = 1;
  int p = v->result.maxSteps;
  if(p == IMPOSSIBLE) return;
  gPoss[p] += v->result.poss;
  p+=TREE_SIZE;
  while(p) {
    gStepsTree[p]++;
    p>>=1;
  }
}

void Deactivate(Vertex *v) {
  if(v->active != 1) return;
  v->active = 0;
  int p = v->result.maxSteps;
  if(p==IMPOSSIBLE) return;
  gPoss[p] -= v->result.poss;
  p+=TREE_SIZE;
  while(p) {
    gStepsTree[p]--;
    p>>=1;
  }
}

void Kill(Vertex *v) {
  Deactivate(v);
  v->active = -1;
}

Result SumActive() {
  Result res;
  int p = 1;
  if(gStepsTree[p]==0) {
    res.maxSteps = IMPOSSIBLE;
    res.poss = 0;
    return res;
  }
  while(p<TREE_SIZE) {
    p<<=1;
    if(gStepsTree[p+1]) ++p;
  }
  p -= TREE_SIZE;
  res.maxSteps = p;
  res.poss = gPoss[p];
  return res;
}

void UpdateInHereStart(Length *length) {
  length->inHereStart.Extract();
  if(!length->headStart.Empty()) {
    Vertex *v = CONTAINER(length->headStart.next, Vertex, inLengthStartOrActive);
    int pos = (v-gVertices) + (length - gLengths);
    if(pos<=N) {
      gHere[pos].headLengthsStart.InsertBefore(&length->inHereStart);
    }
  }
}

void UpdateInHereEnd(Length *length) {
  length->inHereEnd.Extract();
  if(!length->headEnd.Empty()) {
    Vertex *v = CONTAINER(length->headEnd.next, Vertex, inLengthEnd);
    int pos = (v-gVertices) + (length - gLengths);
    if(pos<=N) {
      gHere[pos].headLengthsEnd.InsertBefore(&length->inHereEnd);
    }
  }
}

void IncreaseTo(int alpha, int x) {
  Linked *lengthL = &gHeadLengthsStartOrActive;
  while(lengthL->next != &gHeadLengthsStartOrActive) {
    Length *length = CONTAINER(lengthL->next, Length, inLengthsStartOrActive);
    int len = length - gLengths;
    if(len>=alpha) break;
    lengthL = lengthL->next;
  }
  while(lengthL != &gHeadLengthsStartOrActive) {
    Linked *prev = lengthL->prev;
    lengthL->Extract();
    Length *length = CONTAINER(lengthL, Length, inLengthsStartOrActive);
    length->inHereStart.Extract();
    while(!length->headActive.Empty()) {
      Vertex *v = CONTAINER(length->headActive.prev, Vertex, inLengthStartOrActive);
      int vx = v - gVertices;
      if(vx+alpha <= x+1) break;
      v->inLengthStartOrActive.Extract();
      Deactivate(v);
      if(v->active==0) length->headStart.InsertAfter(&v->inLengthStartOrActive);
    }
    gLengths[alpha].headActive.SpliceBefore(&length->headActive);
    gLengths[alpha].headStart.SpliceBefore(&length->headStart);
    lengthL = prev;
  }
  gLengths[alpha].headStart.InsertBefore(&gVertices[x].inLengthStartOrActive);
  if(gLengths[alpha].inLengthsStartOrActive.Empty()) gHeadLengthsStartOrActive.InsertAfter(&gLengths[alpha].inLengthsStartOrActive);
  UpdateInHereStart(&gLengths[alpha]);
}

void DecreaseTo(int beta, int x) {
  Linked *lengthL = &gHeadLengthsEnd;
  while(lengthL->prev != &gHeadLengthsEnd) {
    Length *length = CONTAINER(lengthL->prev, Length, inLengthsEnd);
    int len = length - gLengths;
    if(len<=beta) break;
    lengthL = lengthL->prev;
  }
  while(lengthL != &gHeadLengthsEnd) {
    Linked *next = lengthL->next;
    lengthL->Extract();
    Length *length = CONTAINER(lengthL, Length, inLengthsEnd);
    length->inHereEnd.Extract();
    gLengths[beta].headEnd.SpliceBefore(&length->headEnd);
    lengthL = next;
  }
  gLengths[beta].headEnd.InsertBefore(&gVertices[x].inLengthEnd);
  for(;;) {
    Vertex *v = CONTAINER(gLengths[beta].headEnd.next, Vertex, inLengthEnd);
    int vx = (v - gVertices);
    if(vx+beta > x) break;
    v->inLengthEnd.Extract();
    Kill(v);
  }
  if(gLengths[beta].inLengthsEnd.Empty()) gHeadLengthsEnd.InsertBefore(&gLengths[beta].inLengthsEnd);
  UpdateInHereEnd(&gLengths[beta]);
}

void ProcessStarts(int x) {
  for(Linked *lengthL = gHere[x].headLengthsStart.next; lengthL != &gHere[x].headLengthsStart; ) {
    Linked *next = lengthL->next;
    Length *length = CONTAINER(lengthL, Length, inHereStart);
    if(!length->headStart.Empty()) {
      Vertex *v = CONTAINER(length->headStart.next, Vertex, inLengthStartOrActive);
      v->inLengthStartOrActive.Extract();
      //assert((v-gVertices) + (length - gLengths) == x);
      Activate(v);
      if(v->active == 1) {
        length->headActive.InsertBefore(&v->inLengthStartOrActive);
      }
    }
    UpdateInHereStart(length);
    lengthL = next;
  }
}

void ProcessEnds(int x) {
  for(Linked *lengthL = gHere[x].headLengthsEnd.next; lengthL != &gHere[x].headLengthsEnd; ) {
    Linked *next = lengthL->next;
    Length *length = CONTAINER(lengthL, Length, inHereEnd);
    if(!length->headEnd.Empty()) {
      Vertex *v = CONTAINER(length->headEnd.next, Vertex, inLengthEnd);
      //assert((v-gVertices) + (length - gLengths) == x);
      v->inLengthEnd.Extract();
      Kill(v);
    }
    UpdateInHereEnd(length);
    lengthL = next;
  }
}

void ComputeAnswer(int x) {
  gVertices[x].result = SumActive();
  if(gVertices[x].result.maxSteps != IMPOSSIBLE) {
    gVertices[x].result.maxSteps++;
  }
}

void ReadAndProcess(int x) {
  int alpha, beta; IN(alpha, beta);
  if(alpha>beta) swap(alpha,beta);
  IncreaseTo(alpha, x);
  DecreaseTo(beta, x);
  ProcessStarts(x+1);
  ComputeAnswer(x+1);
  ProcessEnds(x+1);
}

void WriteAnswer() {
  Result res = gVertices[N].result;
  if(res.maxSteps == IMPOSSIBLE) {
    cout << "NIE\n";
  } else {
    cout << res.maxSteps << ' ' << res.poss.get() << '\n';
  }
}

int main() {
  Initialize();
  REP(x,N) ReadAndProcess(x);
  WriteAnswer();
}