logo AlgoBeat OnlineJudge
登录 注册

#215821. [JAG 2025 Summer Camp #1] LOGCFL

内存限制:1024 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

You are given a 3-dimensional integer array of size .

Initialize the following variables:

integer x = 0;
integer w = 1;
stack s = {};

Then, for each , choose an integer such that and do the following action:

If , do the following:

w *= A[t][x][y];
s.push(x);
x = y;

The above denotes .

If , do the following:

assert (!s.empty());
w *= A[t][x][s.top()];
x = (x + s.top()) % N;
s.pop();

You can't choose if the stack is empty before the action.

Note that the stack has the following operations:

  • push(x): adds an element to the collection.
  • pop(): removes the most recently added element.
  • top(): returns the value of the most recently added element.

For each , consider all possible sequences such that the final value of is . Compute the sum of the corresponding values of over all such sequences, and output the result modulo .

输入格式

The input is given in the following format:

means the value of .

  • All input values are integers.

输出格式

Output lines. On the -th line (), output the answer for .

样例

样例输入 1

2
1 10
100 1000
1 3
9 27

样例输出 1

92
363

样例输入 2

3
2 1 2
1 2 1
1 1 1
2 1 1
2 2 1
2 2 2
2 1 1
1 2 1
1 2 2

样例输出 2

63
68
56

样例输入 3

4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

样例输出 3

120
120
120
120