def f(n)
  return 0 if n==0
  r=1
  for i in 2..n
    r=r*i
  end
  r
end

n=gets.chomp.to_i
p=gets.chomp.split(" ").map{|s| s.to_i}
q=gets.chomp.split(" ").map{|s| s.to_i}
a=1
b=1

c=(1..n).to_a
for i in 0..n-1
  for j in 0..c.size-1
    if p[i]==c[j]
      r=j
    end
  end
  c.delete(p[i])
  a+=f(n-i-1)*r
end

c=(1..n).to_a
for i in 0..n-1
  for j in 0..c.size-1
    if q[i]==c[j]
      r=j
    end
  end
  c.delete(q[i])
  b+=f(n-i-1)*r
end
p (a-b).abs
