a = int(eval(input()))

l = list(map(int,input().split()))

MOD = 998244353

dp = [0]*a

for i in l:

    dp[i]+=1

res = 1

# print(dp)

if  l[0]!=0:

    print((0))

    exit()



for i in l[1:]:

    # print(i)

    res*=dp[i-1]

    res%=MOD



print((res%MOD))
a = int(eval(input()))

l = list(map(int,input().split()))

d = [0]*a

for i in l:

    d[i]+=1

r =1

if  l[0]!=0:

    print((0))

    exit()

for i in l[1:]:

    r=r*d[i-1]%998244353

print(r)
a = int(eval(input()))
l = list(map(int, input().split()))
MOD = 998244353
dp = [0] * a
for i in l:
    dp[i] += 1
res = 1
# print(dp)
if l[0] != 0:
    print((0))
    exit()
for i in l[1:]:
    # print(i)
    res *= dp[i - 1]
    res %= MOD
print((res % MOD))

a = int(eval(input()))
l = list(map(int, input().split()))
d = [0] * a
for i in l:
    d[i] += 1
r = 1
if l[0] != 0:
    print((0))
    exit()
for i in l[1:]:
    r = r * d[i - 1] % 998244353
print(r)

-MOD = 998244353 -dp = [0] * a +d = [0] * a -    dp[i] += 1 -res = 1 -# print(dp) +    d[i] += 1 +r = 1 -    # print(i) -    res *= dp[i - 1] -    res %= MOD -print((res % MOD)) +    r = r * d[i - 1] % 998244353 +print(r)