CF BUDDY
← Problems·

1866B · Battling with Numbers

1400 · combinatorics, math, number theory

Problem: On the trip to campus during the mid semester exam period, Chaneka thinks of two positive integers XX and YY. Since the two integers can be very big, both are represented using their prime factorisations, such that:

  • X=A1B1×A2B2××ANBNX=A_1^{B_1}\times A_2^{B_2}\times\ldots\times A_N^{B_N} (each AiA_i is prime, each BiB_i is positive, and A1<A2<<ANA_1<A_2<\ldots<A_N)
  • Y=C1D1×C2D2××CMDMY=C_1^{D_1}\times C_2^{D_2}\times\ldots\times C_M^{D_M} (each CjC_j is prime, each DjD_j is positive, and C1<C2<<CMC_1<C_2<\ldots<C_M)

Chaneka ponders about these two integers for too long throughout the trip, so Chaneka's friend commands her "Gece, deh!" (move fast) in order to not be late for the exam.

Because of that command, Chaneka comes up with a problem, how many pairs of positive integers pp and qq such that LCM(p,q)=X\text{LCM}(p, q) = X and GCD(p,q)=Y\text{GCD}(p, q) = Y. Since the answer can be very big, output the answer modulo 998244353998\,244\,353.

Notes:

  • LCM(p,q)\text{LCM}(p, q) is the smallest positive integer that is simultaneously divisible by pp and qq.
  • GCD(p,q)\text{GCD}(p, q) is the biggest positive integer that simultaneously divides pp and qq.

Input Format: The first line contains a single integer NN (1N1051 \leq N \leq 10^5) — the number of distinct primes in the prime factorisation of XX.

The second line contains NN integers A1,A2,A3,,ANA_1, A_2, A_3, \ldots, A_N (2A1<A2<<AN21062 \leq A_1 < A_2 < \ldots < A_N \leq 2 \cdot 10^6; each AiA_i is prime) — the primes in the prime factorisation of XX.

The third line contains NN integers B1,B2,B3,,BNB_1, B_2, B_3, \ldots, B_N (1Bi1051 \leq B_i \leq 10^5) — the exponents in the prime factorisation of XX.

The fourth line contains a single integer MM (1M1051 \leq M \leq 10^5) — the number of distinct primes in the prime factorisation of YY.

The fifth line contains MM integers C1,C2,C3,,CMC_1, C_2, C_3, \ldots, C_M (2C1<C2<<CM21062 \leq C_1 < C_2 < \ldots < C_M \leq 2 \cdot 10^6; each CjC_j is prime) — the primes in the prime factorisation of YY.

The sixth line contains MM integers D1,D2,D3,,DMD_1, D_2, D_3, \ldots, D_M (1Dj1051 \leq D_j \leq 10^5) — the exponents in the prime factorisation of YY.

Output Format: An integer representing the number of pairs of positive integers pp and qq such that LCM(p,q)=X\text{LCM}(p, q) = X and GCD(p,q)=Y\text{GCD}(p, q) = Y, modulo 998244353998\,244\,353.

Note: In the first example, the integers are as follows:

  • X=22×31×51×72=2940X=2^2\times3^1\times5^1\times7^2=2940
  • Y=31×71=21Y=3^1\times7^1=21

The following are all possible pairs of pp and qq:

  • p=21p=21, q=2940q=2940
  • p=84p=84, q=735q=735
  • p=105p=105, q=588q=588
  • p=147p=147, q=420q=420
  • p=420p=420, q=147q=147
  • p=588p=588, q=105q=105
  • p=735p=735, q=84q=84
  • p=2940p=2940, q=21q=21

In the third example, the integers are as follows:

  • X=21×51=10X=2^1\times5^1=10
  • Y=21×31=6Y=2^1\times3^1=6

There is no pair pp and qq that simultaneously satisfies LCM(p,q)=10\text{LCM}(p,q)=10 and GCD(p,q)=6\text{GCD}(p,q)=6.

Sample Cases

Case 1

Input

4
2 3 5 7
2 1 1 2
2
3 7
1 1

Output

8

Case 2

Input

2
1299721 1999993
100000 265
2
1299721 1999993
100000 265

Output

1

Case 3

Input

2
2 5
1 1
2
2 3
1 1

Output

0

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback