import java.io.*; import java.util.*; class Solver { private int[] a, u, d; private int n; private Solver(int[] u, int[] d) { if(u.length != d.length) { throw new IllegalArgumentException("Length mismatch"); } this.n = u.length; this.a = new int[n]; this.u = u; this.d = d; for(int i = 0; i < n; i++) { a[i] = 0; } } private int solveLower(int m, int k) { int[] u = new int[n]; int[] d = new int[n]; int ui = 1; int di = 1; for(int i = n - 1; i >= 0; i--) { u[i] = ui; d[i] = di; if(a[i] == 0 || a[i] > m) { ui = 1; ++di; } else { di = 1; ++ui; } } boolean flag = true; int c = 0; for(int i = 0; i < n; i++) { if(flag && a[i] == 0 && u[i] == this.u[i] && d[i] == this.d[i]) { a[i] = m; c += solve(k + 1); a[i] = 0; flag = false; } if(a[i] != 0 && a[i] < m) { flag = true; } } return c; } private int solveUpper(int m, int k) { int[] u = new int[n]; int[] d = new int[n]; int ui = 1; int di = 1; for(int i = n - 1; i >= 0; i--) { u[i] = ui; d[i] = di; if(a[i] == 0 || a[i] < m) { di = 1; ++ui; } else { ui = 1; ++di; } } boolean flag = true; int c = 0; for(int i = 0; i < n; i++) { if(flag && a[i] == 0 && u[i] == this.u[i] && d[i] == this.d[i]) { a[i] = m; c += solve(k + 1); a[i] = 0; flag = false; } if(a[i] != 0 && a[i] > m) { flag = true; } } return c; } private int solve(int k) { if(k == n) { return 1; } if(k % 2 == 0) { return solveLower(k / 2 + 1, k); } else { return solveUpper(n - k / 2, k); } } public static int solve(int[] u, int[] d) { Solver s = new Solver(u, d); return s.solve(0); } } public class updown { public static void main(String[] args) throws Exception { BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(System.in)); while(true) { int n = Integer.parseInt(br.readLine()); if(n == 0) break; StringTokenizer tok; tok = new StringTokenizer(br.readLine()); int[] u = new int[n]; for(int i = 0; i < n; i++) { u[i] = Integer.parseInt(tok.nextToken()); } tok = new StringTokenizer(br.readLine()); int[] d = new int[n]; for(int i = 0; i < n; i++) { d[i] = Integer.parseInt(tok.nextToken()); } long st = System.currentTimeMillis(); System.out.println(Solver.solve(u, d)); long et = System.currentTimeMillis(); System.out.println((et - st) + "ms"); } } finally { if(br != null) br.close(); } } }