PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000334. Đếm đường đi ngắn nhất trên đồ thị các ước (Divisor Paths)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm đường đi ngắn nhất trên đồ thị các ước (Divisor Paths)

Divisor Paths

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given a positive integer DD, construct an undirected graph whose vertices are all positive divisors of DD.

Two vertices x>yx>y are joined by an edge when y∣xy\mid x and x/yx/y is prime. The edge weight equals the number of positive divisors of xx that are not divisors of yy. A path's length is the sum of edge weights; the empty path from a vertex to itself has length zero.

For each of qq queries, two vertices v,uv,u (both divisors of DD) are given. Count the shortest paths from vv to uu. Paths with different edge sequences are distinct. Print each answer modulo 998244353998244353.

Input

The first line is DD, the second is qq, and each of the next qq lines contains v,uv,u, both guaranteed divisors of DD.

Output

Print qq lines, one shortest-path count per query modulo 998244353998244353.

Subtasks

  • Subtask 1 (20 points): 1≤D≤10001\le D\le1000, 1≤q≤301\le q\le30, v,u∣Dv,u\mid D.
  • Subtask 2 (30 points): 1≤D≤1091\le D\le10^9, 1≤q≤30001\le q\le3000, v,u∣Dv,u\mid D.
  • Subtask 3 (50 points): 1≤D≤10151\le D\le10^{15}, 1≤q≤3⋅1051\le q\le 3\cdot10^5, v,u∣Dv,u\mid D.

Examples

Example 1

Input

12
3
4 4
12 1
3 4

Output

1
3
1

Explanation

For a query from vertex 44 to itself, the unique shortest path is the empty path, of weight 00. From 1212 to 11, the three shortest vertex sequences are 12,4,2,112,4,2,1; 12,6,2,112,6,2,1; and 12,6,3,112,6,3,1. From 33 to 44, the only shortest sequence is 3,1,2,43,1,2,4.

Example 2

Input

1
1
1 1

Output

1

Explanation

When D=1D=1, the graph has one vertex, with one empty path to itself.

Example 3

Input

288807105787200
4
46 482955026400
12556830686400 897
414 12556830686400
4443186242880 325

Output

547558588
277147129
457421435
702277623

Explanation

The large queries require modular combinatorial counting; direct path enumeration is infeasible.

Note: Each input file contains one case using the specified format.