import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws Exception {
		int t = 1;
		while (t-- > 0) {
			int n = ni();
			long a[] = nla(n);
			ps(1, n);
			pln();
			for (int i = 0; i < n; i++) {
				ps(-n * a[i]);
			}
			pln();
			ps(1, n - 1);
			pln();
			for (int i = 0; i < n - 1; i++) {
				ps((n - 1) * a[i]);
			}
			pln();
			ps(2, n);
			pln();
			for (int i = 1; i < n - 1; i++)
				ps("0");
			ps((n - 1) * a[n - 1]);
			pln();
		}
		flush();
	}

	static Reader in = new Reader();
	static StringBuilder output = new StringBuilder();
	static final int imod = 1000_000_007;
	static final long mod = 1000_000_007;
	static final int imax = 1000_000_000;
	static final int imin = -1000_000_000;
	static final long lmax = 1000_000_000_000_000_000L;
	static final long lmin = -1000_000_000_000_000_000L;

	//input functions
	static int ni() {return Integer.parseInt(in.next());}
	static long nl() {return Long.parseLong(in.next());}
	static String ns() {return in.next();}
	static double nd() {return Double.parseDouble(in.next());}
	static int[] eia(int n) {int a[] = new int[n]; return a;}
	static long[] ela(int n) {long a[] = new long[n]; return a;}
	static double[] eda(int n) {double a[] = new double[n]; return a;}
	static String[] esa(int n) {String a[] = new String[n]; return a;}
	static int[] nia(int n) {int a[] = new int[n]; for (int i = 0; i < n; i++)a[i] = ni(); return a;}
	static long[] nla(int n) {long a[] = new long[n]; for (int i = 0; i < n; i++)a[i] = nl(); return a;}
	static String[] nsa(int n) {String a[] = new String[n]; for (int i = 0; i < n; i++)a[i] = ns(); return a;}
	static double[] nda(int n) {double a[] = new double[n]; for (int i = 0; i < n; i++)a[i] = nd(); return a;}

	//output functions
	static void print(String a[]) {for (String i : a) System.out.print(i + " "); System.out.println();}
	static void print(int a[]) {for (int i : a) System.out.print(i + " "); System.out.println();}
	static void print(long a[]) {for (long i : a) System.out.print(i + " "); System.out.println();}
	static void print(double a[]) {for (double i : a) System.out.print(i + " "); System.out.println();}
	static void print(Object... a) {for (Object i : a) {System.out.print(i); System.out.print(" ");} System.out.println();}
	static void println(Object... a) {for (Object i : a) {System.out.print(i); System.out.print("\n");}}
	static void println() {System.out.println();}
	static void flush() {System.out.print(output); output = new StringBuilder();}
	static void pln(int []a) {for (int i : a) {output.append(i).append('\n');}}
	static void pln(long []a) {for (long i : a) {output.append(i).append('\n');}}
	static void pln(double []a) {for (double i : a) {output.append(i).append('\n');}}
	static void pln(String []a) {for (String i : a) {output.append(i).append('\n');}}
	static void pln(Object...a) {for (Object i : a) {output.append(i).append('\n');}}
	static void pln() {output.append('\n');}
	static void p(int []a) {for (int i : a) {output.append(i);}}
	static void p(long []a) {for (long i : a) {output.append(i);}}
	static void p(double []a) {for (double i : a) {output.append(i);}}
	static void p(String []a) {for (String i : a) {output.append(i);}}
	static void p(Object[] a) {for (Object i : a) {output.append(i);}}
	static void ps() {output.append(" ");}
	static void ps(int []a) {for (int i : a) {output.append(i).append(" ");}}
	static void ps(long []a) {for (long i : a) {output.append(i).append(" ");}}
	static void ps(double []a) {for (double i : a) {output.append(i).append(" ");}}
	static void ps(String []a) {for (String i : a) {output.append(i).append(" ");}}
	static void ps(Object... a) {for (Object i : a) {output.append(i).append(" ");}}

	//Utility functions
	static int lowerbound(int[]a, int key) { // returns index of smallest element larger than equal to given element between indices l and r inclusive
		int low = 0, high = a.length - 1, mid = (low + high) / 2; while (low < high) {if (a[mid] >= key)high = mid; else low = mid + 1; mid = (low + high) / 2;} return mid;
	}
	static int upperbound(int[]a, int key) {int low = 0, high = a.length - 1, mid = (low + high + 1) / 2; while (low < high) {if (a[mid] <= key) low = mid; else high = mid - 1; mid = (low + high + 1) / 2;} return mid;}
	static int lowerbound(int[]a, int l, int r, int key) { int low = l, high = r, mid = (low + high) / 2; while (low < high) { if (a[mid] >= key) high = mid; else low = mid + 1; mid = (low + high) / 2;} return mid;}
	static int upperbound(int[]a, int l, int r, int key) { // returns index of largest element smaller than equal to given element
		int low = l, high = r, mid = (low + high + 1) / 2; while (low < high) { if (a[mid] <= key) low = mid; else high = mid - 1; mid = (low + high + 1) / 2;} return mid;
	}

	static long powm(long a, long b, long mod) {long an = 1; long c = a % mod; while (b > 0) {if (b % 2 == 1)an = (an * c) % mod; c = (c * c) % mod; b >>= 1;} return an;}
	static long powm(long a, long b) {long an = 1; long c = a % mod; while (b > 0) {if (b % 2 == 1)an = (an * c) % mod; c = (c * c) % mod; b >>= 1;} return an;}
	static long pow(long a, long b) {long an = 1; long c = a; while (b > 0) {if (b % 2 == 1)an *= c; c *= c; b >>= 1;} return an;}
	static void reverse(int[]a) {for (int i = 0; i < a.length / 2; i++) {a[i] ^= a[a.length - i - 1]; a[a.length - i - 1] ^= a[i]; a[i] ^= a[a.length - i - 1];}}
	static void exit() {System.exit(0);}
	static int min(int... a) {int min = a[0]; for (int i : a)min = Math.min(min, i); return min;}
	static long min(long... a) {long min = a[0]; for (long i : a)min = Math.min(min, i); return min;}
	static double min(double... a) {double min = a[0]; for (double i : a)min = Math.min(min, i); return min;}
	static double max(double... a) {double max = a[0]; for (double i : a)max = Math.max(max, i); return max;}
	static int max(int... a) {int max = a[0]; for (int i : a)max = Math.max(max, i); return max;}
	static long max(long... a) {long max = a[0]; for (long i : a)max = Math.max(max, i); return max;}
	static long gcd(long... a) {long gcd = a[0]; for (long i : a)gcd = gcd(gcd, i); return gcd;}
	static int gcd(int... a) {int gcd = a[0]; for (int i : a)gcd = gcd(gcd, i); return gcd;}
	static int gcd(int a, int b) {if (b == 0)return a; if (a == 0) return b; return gcd(b, a % b);}
	static long gcd(long a, long b) {if (b == 0)return a; if (a == 0) return b; return gcd(b, a % b);}

	static class Pair {
		long la, lb;
		int a, b;
		double da, db;
		Pair() {} Pair(int c, int d) {a = c; b = d;}
		Pair(long c, long d) {la = c; lb = d;}
		Pair(int c, long d) { a = c; lb = d; }
		Pair(long c, int d) { la = c; b = d; }
		Pair(int c, double d) { a = c; db = d; }
		Pair(double c, double d) { da = c; db = d; }
		Pair(double c, int d) { da = c; b = d; }

		@Override
		public int hashCode() { return (a + " " + b).hashCode(); }
		public boolean equals(Object c) { return (a == (((Pair)c).a) && b == (((Pair)c).b)); }
	}

	static class Reader {
		public BufferedReader reader;
		public StringTokenizer tokenizer;
		public Reader() {
			reader = new BufferedReader(new InputStreamReader(System.in));
			tokenizer = null;
		}
		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}
	}
}
class AL<T> extends java.util.ArrayList<T> {}
class HM<T, L> extends java.util.HashMap<T, L> {}
class HS<T> extends java.util.HashSet<T> {}


import java.io.*;
import java.util.*;

public class A {

	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int n = sc.nextInt();
		long[] arr = new long[n];
		long[] g = gcdex(n, n-1);
		for(int i = 0; i < n; i++){
			arr[i] = sc.nextInt();
		}
		//1
		StringBuilder sb = new StringBuilder();
		sb.append(1+" "+n+"\n");
		for(int i = 0; i < n; i++) {
			sb.append((-arr[i]*g[0]*n));
			if(i < n) sb.append(" ");
		}
		sb.append("\n"+2+" "+n+"\n");
		for(int i = 1; i < n; i++) {
			sb.append((-arr[i]*g[1]*(n-1)));
			if(i < n) sb.append(" ");
		}
		sb.append("\n"+1+" "+1+"\n"+(-arr[0]+arr[0]*g[0]*n));
		PrintWriter pw = new PrintWriter(System.out);
		pw.print(sb.toString());
		pw.flush();
	}

	static long[] gcdex(long a, long b){
		if(b > a) {
			long[] p = gcdex(b, a);
			return new long[] {p[1], p[0]};
		}
		else if(b == 0) return new long[] {1, 0};
		else{
			long[] p = gcdex(b, a % b);
			return new long[] {p[1], p[0] - p[1]*(a/b)};
		}
	}

	static class FastScanner {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        public FastScanner() {
            reader = new BufferedReader(new InputStreamReader(System.in), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
        	return Long.parseLong(next());
        }
        public double nextDouble() {
        	return Double.parseDouble(next());
        }
        public String nextLine() {
        	try {
        		return reader.readLine();
        	} catch(IOException e) {
        		throw new RuntimeException(e);
        	}
        }
    }

}


import java.io.*;
import java.util.*;

public class A {

	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int n = sc.nextInt();
		long[] arr = new long[n];
		long[] g = gcdex(n, n-1);
		for(int i = 0; i < n; i++){
			arr[i] = sc.nextInt();
		}
		if(n == 1) {
			System.out.println("1 1");
			System.out.println(-arr[0]);
			System.out.println("1 1");
			System.out.println("0");
			System.out.println("1 1");
			System.out.println("0");
		}
		else {
			StringBuilder sb = new StringBuilder();
			sb.append(1+" "+n+"\n");
			for(int i = 0; i < n; i++) {
				sb.append((-arr[i]*g[0]*n));
				if(i < n) sb.append(" ");
			}
			sb.append("\n"+2+" "+n+"\n");
			for(int i = 1; i < n; i++) {
				sb.append((-arr[i]*g[1]*(n-1)));
				if(i < n) sb.append(" ");
			}
			sb.append("\n"+1+" "+1+"\n"+(-arr[0]+arr[0]*g[0]*n));
			PrintWriter pw = new PrintWriter(System.out);
			pw.print(sb.toString());
			pw.flush();
		}
	}

	static long[] gcdex(long a, long b){
		if(b > a) {
			long[] p = gcdex(b, a);
			return new long[] {p[1], p[0]};
		}
		else if(b == 0) return new long[] {1, 0};
		else{
			long[] p = gcdex(b, a % b);
			return new long[] {p[1], p[0] - p[1]*(a/b)};
		}
	}

	static class FastScanner {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        public FastScanner() {
            reader = new BufferedReader(new InputStreamReader(System.in), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
        	return Long.parseLong(next());
        }
        public double nextDouble() {
        	return Double.parseDouble(next());
        }
        public String nextLine() {
        	try {
        		return reader.readLine();
        	} catch(IOException e) {
        		throw new RuntimeException(e);
        	}
        }
    }

}


import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Map.Entry;

import java.util.Random;
import java.util.TreeSet;

public final class CF_666_D1_A
{

	static boolean verb=true;
	static void log(Object X){if (verb) System.err.println(X);}
	static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}}
	static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}}
	static void logWln(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");}}
	static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}}
	static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}}

	static void logWln(Object X){if (verb) System.err.print(X);}
	static void info(Object o){	System.out.println(o);}
	static void output(Object o){outputWln(""+o+"\n");	}
	static void outputFlush(Object o){try {out.write(""+ o+"\n");out.flush();} catch (Exception e) {}}
	static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}}

	static void logBin(int[] tm) {for (int x:tm) logWln(bin(8,x)+" ");log("");}

	static String bin(int L,int x) {
		String s=Integer.toBinaryString(x);
		while (s.length()<L) s="0"+s;
		return s;
	}

	static long powerMod(long b,long e,long m){
		long x=1;
		while (e>0) {
			if (e%2==1)
				x=(b*x)%m;
			b=(b*b)%m;
			e=e/2;
		}
		return x;
	}

	static class Composite implements Comparable<Composite>{

		int val;
		int idx;

		public int compareTo(Composite X) {
			if (val!=X.val)
				return val-X.val;
			return idx-X.idx;
		}

		public Composite(int val, int idx) {
			this.val = val;
			this.idx = idx;
		}

	}

	static long mod=1000000007;

	// Global vars
	static BufferedWriter out;
	static InputReader reader;

	static int[][] dd={{-1,0},{0,1},{1,0},{0,-1}};

	static int CX=26;

	static void process() throws Exception {

		out = new BufferedWriter(new OutputStreamWriter(System.out));
		reader = new InputReader(System.in);

		int n=reader.readInt();
		long[] a=new long[n];
		for (int i=0;i<n;i++)
			a[i]=reader.readLong();

		if (n==1) {
			output(" 1 1");
			output(-a[0]);
			output(" 1 1");
			output(0);
			output(" 1 1");
			output(0);
		} else {
			output(1+" "+(n-1));
			String s="";
			for (int i=0;i<n-1;i++) {
				s+=a[i]*(n-1)+" ";
				a[i]+=a[i]*(n-1);
			}
			output(s);
			output(2+" "+n);
			s="";
			for (int i=1;i<n;i++) {
				s+=a[i]*(n-1)+" ";
				a[i]+=a[i]*(n-1);
			}
			output(s);
			output(1+" "+n);
			//log(a);

			s="";
			for (int i=0;i<n;i++) {
				long x=a[i];
				long delta=-x/n;
				//log(x+" "+delta+" "+n);
				s+=(delta*n)+" ";
				a[i]+=delta*n;
			}
			output(s);
			//log(a);

		}

		try {
			out.close();
		} catch (Exception Ex) {
		}

	}

	public static void main(String[] args) throws Exception {
		process();

	}

	static final class InputReader {
		private final InputStream stream;
		private final byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		private int read() throws IOException {
			if (curChar >= numChars) {
				curChar = 0;
				numChars = stream.read(buf);
				if (numChars <= 0) {
					return -1;
				}
			}
			return buf[curChar++];
		}

		public final String readString() throws IOException {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.append((char) c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public final String readString(int L) throws IOException {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res = new StringBuilder(L);
			do {
				res.append((char) c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public final int readInt() throws IOException {
			int c = read();
			boolean neg = false;
			while (isSpaceChar(c)) {
				c = read();
			}
			char d = (char) c;
			// log("d:"+d);
			if (d == '-') {
				neg = true;
				c = read();
			}
			int res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			// log("res:"+res);
			if (neg)
				return -res;
			return res;

		}

		public final long readLong() throws IOException {
			int c = read();
			boolean neg = false;
			while (isSpaceChar(c)) {
				c = read();
			}
			char d = (char) c;
			// log("d:"+d);
			if (d == '-') {
				neg = true;
				c = read();
			}
			long res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			// log("res:"+res);
			if (neg)
				return -res;
			return res;

		}

		private boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}
	}

}


import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Map.Entry;

import java.util.Random;
import java.util.TreeSet;

public final class CF_666_D1_A
{

	static boolean verb=true;
	static void log(Object X){if (verb) System.err.println(X);}
	static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}}
	static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}}
	static void logWln(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");}}
	static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}}
	static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}}

	static void logWln(Object X){if (verb) System.err.print(X);}
	static void info(Object o){	System.out.println(o);}
	static void output(Object o){outputWln(""+o+"\n");	}
	static void outputFlush(Object o){try {out.write(""+ o+"\n");out.flush();} catch (Exception e) {}}
	static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}}

	static void logBin(int[] tm) {for (int x:tm) logWln(bin(8,x)+" ");log("");}

	static String bin(int L,int x) {
		String s=Integer.toBinaryString(x);
		while (s.length()<L) s="0"+s;
		return s;
	}

	static long powerMod(long b,long e,long m){
		long x=1;
		while (e>0) {
			if (e%2==1)
				x=(b*x)%m;
			b=(b*b)%m;
			e=e/2;
		}
		return x;
	}

	static class Composite implements Comparable<Composite>{

		int val;
		int idx;

		public int compareTo(Composite X) {
			if (val!=X.val)
				return val-X.val;
			return idx-X.idx;
		}

		public Composite(int val, int idx) {
			this.val = val;
			this.idx = idx;
		}

	}

	static long mod=1000000007;

	// Global vars
	static BufferedWriter out;
	static InputReader reader;

	static int[][] dd={{-1,0},{0,1},{1,0},{0,-1}};

	static int CX=26;

	static void process() throws Exception {

		out = new BufferedWriter(new OutputStreamWriter(System.out));
		reader = new InputReader(System.in);

		int n=reader.readInt();
		long[] a=new long[n];
		for (int i=0;i<n;i++)
			a[i]=reader.readLong();

		if (n==1) {
			output(" 1 1");
			output(-a[0]);
			output(" 1 1");
			output(0);
			output(" 1 1");
			output(0);
		} else {
			output(1+" "+(n-1));
			String s="";
			for (int i=0;i<n-1;i++) {
				s+=a[i]*(n-1)+" ";
				a[i]+=a[i]*(n-1);
			}
			output(s);
			output(2+" "+n);
			s="";
			for (int i=1;i<n;i++) {
				s+=a[i]*(n-1)+" ";
				a[i]+=a[i]*(n-1);
			}
			output(s);
			output(1+" "+n);
			//log(a);

			s="";
			for (int i=0;i<n;i++) {
				long x=a[i];
				long delta=-x/n;
				//log(x+" "+delta+" "+n);
				s+=(delta)+" ";
				a[i]+=delta*n;
			}
			output(s);
			//log(a);

		}

		try {
			out.close();
		} catch (Exception Ex) {
		}

	}

	public static void main(String[] args) throws Exception {
		process();

	}

	static final class InputReader {
		private final InputStream stream;
		private final byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		private int read() throws IOException {
			if (curChar >= numChars) {
				curChar = 0;
				numChars = stream.read(buf);
				if (numChars <= 0) {
					return -1;
				}
			}
			return buf[curChar++];
		}

		public final String readString() throws IOException {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.append((char) c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public final String readString(int L) throws IOException {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res = new StringBuilder(L);
			do {
				res.append((char) c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public final int readInt() throws IOException {
			int c = read();
			boolean neg = false;
			while (isSpaceChar(c)) {
				c = read();
			}
			char d = (char) c;
			// log("d:"+d);
			if (d == '-') {
				neg = true;
				c = read();
			}
			int res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			// log("res:"+res);
			if (neg)
				return -res;
			return res;

		}

		public final long readLong() throws IOException {
			int c = read();
			boolean neg = false;
			while (isSpaceChar(c)) {
				c = read();
			}
			char d = (char) c;
			// log("d:"+d);
			if (d == '-') {
				neg = true;
				c = read();
			}
			long res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			// log("res:"+res);
			if (neg)
				return -res;
			return res;

		}

		private boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}
	}

}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>

#define int long long
#define M 3000010
#define P 1000000007

using namespace std;

vector <int> V[M];

int n, Val[M], Sm[M], Pr[M], F[M], s[M], O[M], t = 0;

bool B[M], vis[M];

void DFS(int x) {
    Sm[x] = Val[x];
    for(int i = 0; i < V[x].size(); i++) DFS(V[x][i]), Sm[x] += Sm[V[x][i]];
}

int getcnt(int x) {
    int w = lower_bound(Sm + 1, Sm + n + 1, x) - Sm;
    if(Sm[w] != x) return 0;
    int o = lower_bound(Sm + 1, Sm + n + 1, x + 1) - Sm;
    return o - w;
}

signed main() {
    for(int i = 2; i <= 3000000; i++) {
        if(!vis[i]) Pr[++t] = i;//, lw[i] = i, s[i] = i;
        for(int j = 1; Pr[j] * i <= 3000000; j++) {
            vis[Pr[j] * i] = 1;
            //lw[Pr[j] * i] = Pr[j];
            s[Pr[j] * i] = Pr[j];
            if(i % Pr[j] == 0) {
                //lw[Pr[j] * i] = lw[i] * Pr[j];
                break;
            }
        }
    }
    scanf("%lld", &n);
    for(int i = 1; i <= n; i++) scanf("%lld", &Val[i]);
    for(int i = 2; i <= n; i++) {
        int a;
        scanf("%lld", &a);
        V[a].push_back(i);
    }
    DFS(1);
    sort(Sm + 1, Sm + n + 1);
    for(int i = 1; i <= n; i++) if(Sm[n] % i == 0) {
        int ans = 0, tmp = Sm[n] / i;
        if(i <= n / 10) for(int j = 1; j <= i; j++) ans += getcnt(tmp * j);
        else for(int j = 1; j <= n; j++) if(Sm[j] % tmp == 0) ans++;
        if(ans == i) B[i] = 1;
    }
    F[1] = 1;
    for(int i = 1; i <= n; i++)
        for(int j = i + i; j <= n; j += i)
            if(B[j]) F[j] = (F[j] + F[i]) % P;
    int ans = 0;
    for(int i = 1; i <= n; i++) ans = (ans + F[i]) % P;
    printf("%lld\n", (ans % P + P) % P);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>

#define int long long
#define M 3000010
#define P 1000000007

using namespace std;

vector <int> V[M];

int n, Val[M], Sm[M], Pr[M], F[M], s[M], O[M], t = 0;

bool B[M], vis[M];

void DFS(int x) {
    Sm[x] = Val[x];
    for(int i = 0; i < V[x].size(); i++) DFS(V[x][i]), Sm[x] += Sm[V[x][i]];
}

int getcnt(int x) {
    int w = lower_bound(Sm + 1, Sm + n + 1, x) - Sm;
    if(Sm[w] != x) return 0;
    int o = lower_bound(Sm + 1, Sm + n + 1, x + 1) - Sm;
    return o - w;
}

signed main() {
    for(int i = 2; i <= 3000000; i++) {
        if(!vis[i]) Pr[++t] = i;//, lw[i] = i, s[i] = i;
        for(int j = 1; Pr[j] * i <= 3000000; j++) {
            vis[Pr[j] * i] = 1;
            //lw[Pr[j] * i] = Pr[j];
            s[Pr[j] * i] = Pr[j];
            if(i % Pr[j] == 0) {
                //lw[Pr[j] * i] = lw[i] * Pr[j];
                break;
            }
        }
    }
    scanf("%lld", &n);
    for(int i = 1; i <= n; i++) scanf("%lld", &Val[i]);
    for(int i = 2; i <= n; i++) {
        int a;
        scanf("%lld", &a);
        V[a].push_back(i);
    }
    DFS(1);
    sort(Sm + 1, Sm + n + 1);
    for(int i = 1; i <= n; i++) if(Sm[n] % i == 0) {
        int ans = 0, tmp = Sm[n] / i;
        for(int j = 1; j <= i; j++) ans += getcnt(tmp * j);
        if(ans == i) B[i] = 1;
    }
    F[1] = 1;
    for(int i = 1; i <= n; i++)
        for(int j = i + i; j <= n; j += i)
            if(B[j]) F[j] = (F[j] + F[i]) % P;
    int ans = 0;
    for(int i = 1; i <= n; i++) ans = (ans + F[i]) % P;
    printf("%lld\n", (ans % P + P) % P);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>

#define int long long
#define M 3000010
#define P 1000000007

using namespace std;

vector <int> V[M];

int n, Val[M], Sm[M], Pr[M], F[M], s[M], O[M], t = 0;

bool B[M], vis[M];

void DFS(int x) {
    Sm[x] = Val[x];
    for(int i = 0; i < V[x].size(); i++) DFS(V[x][i]), Sm[x] += Sm[V[x][i]];
}

int getcnt(int x) {
    int w = lower_bound(Sm + 1, Sm + n + 1, x) - Sm;
    if(Sm[w] != x) return 0;
    int o = lower_bound(Sm + 1, Sm + n + 1, x + 1) - Sm;
    return o - w;
}

signed main() {
    for(int i = 2; i <= 3000000; i++) {
        if(!vis[i]) Pr[++t] = i;//, lw[i] = i, s[i] = i;
        for(int j = 1; Pr[j] * i <= 3000000; j++) {
            vis[Pr[j] * i] = 1;
            //lw[Pr[j] * i] = Pr[j];
            s[Pr[j] * i] = Pr[j];
            if(i % Pr[j] == 0) {
                //lw[Pr[j] * i] = lw[i] * Pr[j];
                break;
            }
        }
    }
    scanf("%lld", &n);
    for(int i = 1; i <= n; i++) scanf("%lld", &Val[i]);
    for(int i = 2; i <= n; i++) {
        int a;
        scanf("%lld", &a);
        V[a].push_back(i);
    }
    DFS(1);
    sort(Sm + 1, Sm + n + 1);
    for(int i = 1; i <= n; i++) if(Sm[n] % i == 0) {
        int ans = 0, tmp = Sm[n] / i;
        for(int j = 1; j <= i; j++) {
            ans += getcnt(tmp * j);
            if(ans < j) break;
        }
        if(ans == i) B[i] = 1;
    }
    F[1] = 1;
    for(int i = 1; i <= n; i++)
        for(int j = i + i; j <= n; j += i)
            if(B[j]) F[j] = (F[j] + F[i]) % P;
    int ans = 0;
    for(int i = 1; i <= n; i++) ans = (ans + F[i]) % P;
    printf("%lld\n", (ans % P + P) % P);
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 1061109567
#define pb push_back
#define in(s) freopen(s,"r",stdin);
#define out(s) freopen(s,"w",stdout);
#define fi first
#define se second
#define bw(i,r,l) for (int i=r-1;i>=l;i--)
#define fw(i,l,r) for (int i=l;i<r;i++)
#define fa(i,x) for (auto i:x)
using namespace std;
const int N = 1e6 + 5;
int n, a[N], dp[N], sum[N], p[N], cnt[N];
signed main() {
	#ifdef aome
	in("aome.inp");
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	fw (i, 0, n) cin >> a[i];
	fw (i, 1, n) cin >> p[i], p[i]--;
	/*
	Divide the original tree into x sections, then each one will have sum / x. A lot of these will
	be subtrees, then it's a subtree erased by smaller subtrees inside. So consider subtrees that
	have a sum as a multiple of (sum / x) only.
	We can also see, there's merely 1 way to distribute the tree for each possible x.
	Now, split the x regions into x * y regions. We can see the plan to do so for x * y will be
	contained in x's.
	So call dp[i]: Number of ways to split a plan with i regions into even more.
	Then it's easy to find the answer.
	Our task now is to find all possible x. Consider a subtree with sum N.
	N = a * sum / x.
	x = a * sum / N. Call d = gcd(sum, N), sum1 = sum / d, N1 = N / d. Then
	x = a * sum1 / N1, gcd(sum1, N1) = 1. So a must be a multiple of N1. Call a = b * N1. Then
	x = b * sum1.
	So x is a multiple of sum1, which is sum / gcd(sum, N). However, to split into x cities there must
	be at least x subtrees satisfying this condition. So we will count it by DP.
	Complexity: O(NlogN)
	*/
	fw (i, 0, n) sum[i] = a[i];
	bw (i, n, 1) sum[p[i]] += sum[i];
	fw (i, 0, n) {
		int val = sum[0] / __gcd(sum[0], sum[i]);
		if (val <= n) cnt[val]++;
	}
	bw (i, n + 1, 1) {
		for (int j = i + i; j <= n; j += i) cnt[j] += cnt[i];
	}
	bw (i, n + 1, 1) if (cnt[i] >= i) {
		dp[i] = 1;
		for (int j = i + i; j <= n; j += i) dp[i] += dp[j];
	}
	cout << dp[1];
	return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 1061109567
#define int long long
#define pb push_back
#define in(s) freopen(s,"r",stdin);
#define out(s) freopen(s,"w",stdout);
#define fi first
#define se second
#define bw(i,r,l) for (int i=r-1;i>=l;i--)
#define fw(i,l,r) for (int i=l;i<r;i++)
#define fa(i,x) for (auto i:x)
using namespace std;
const int N = 1e6 + 5;
int n, a[N], dp[N], sum[N], p[N], cnt[N];
signed main() {
	#ifdef aome
	in("aome.inp");
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	fw (i, 0, n) cin >> a[i];
	fw (i, 1, n) cin >> p[i], p[i]--;
	fw (i, 0, n) sum[i] = a[i];
	bw (i, n, 1) sum[p[i]] += sum[i];
	fw (i, 0, n) {
		int val = sum[0] / __gcd(sum[0], sum[i]);
		if (val <= n) cnt[val]++;
	}
	bw (i, n + 1, 1) {
		for (int j = i + i; j <= n; j += i) cnt[j] += cnt[i];
	}
	bw (i, n + 1, 1) if (cnt[i] >= i) {
		dp[i] = 1;
		for (int j = i + i; j <= n; j += i) dp[i] += dp[j];
	}
	cout << dp[1];
	return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sq(x) ((x)*(x))
using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;

const int inf=0x3f3f3f3f,mod=1000000007;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

inline int power(int a, int b, int m=mod, int ans=1) {
  for(; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
  return ans;
}

const int NN=1000100;
int hd[NN];
int nx[NN<<1];
int to[NN<<1];
int E;

void add(int u,int v){
  nx[E]=hd[u];
  to[E]=v;
  hd[u]=E++;
  nx[E]=hd[v];
  to[E]=u;
  hd[v]=E++;
}

int a[NN];
INT arr[10000],m;
INT sum[NN],key;

bool dfs(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(v==fa) continue;
    if(!dfs(v,u)) return 0;
    sum[u]+=sum[v];
  }
  if(sum[u]>key) return 0;
  if(sum[u]==key) sum[u]=0;
  return 1;
}

int dp[10000];
bool can[1000];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  memset(hd,-1,sizeof(hd));
  int n;cin>>n;
  INT tot=0;
  for(int i=1; i<=n; i++) {
    scanf("%d",a+i);
    tot+=a[i];
  }
  for(int i=2,d; i<=n; i++){
    scanf("%d",&d);
    add(i,d);
  }

  INT tmp=tot;
  for(int i=1; (INT)i*i<=tmp; i++) if(tmp%i==0){
    arr[m++]=i;
    arr[m++]=tmp/i;
  }

  sort(arr,arr+m);
  m=unique(arr,arr+m)-arr;

  for(int i=0; i<m; i++){
    key=arr[i];
    if(!dfs(1)) continue;
    if(sum[1]==0) can[i]=1;
  }

  for(int i=0; i<m; i++){
    if(can[i]){
      dp[i]=1;
      for(int j=0; j<i; j++) if(can[j] && arr[i]%arr[j]==0){
        dp[i]+=dp[j];
        if(dp[i]>=mod) dp[i]-=mod;
      }
    }
  }

  cout<<dp[m-1]<<endl;

  return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sq(x) ((x)*(x))
using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
#define INPUT_LEN 1000
#define gets my_gets
char gchar() {static char tbuf1[INPUT_LEN],*tmp_st=tbuf1,*tmp_ed=tbuf1;if(tmp_st==tmp_ed){int len=fread(tbuf1,1,INPUT_LEN,stdin);if (len==0)return -1;tmp_ed=tbuf1+len;tmp_st=tbuf1;}return*(tmp_st++);}
inline bool gn(int &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(INT &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(char &x) {while(x=gchar(),x!=-1&&isspace(x));return x!=-1;}
inline bool gn(string &s){char c;s.clear();while(c=gchar(),c!=-1&&isspace(c));if(c!=-1)s+=c;while(c=gchar(),c!=-1&&!isspace(c))s+=c;return c!=-1;}
inline bool gn(DO &x) {string s;bool ok=gn(s);x=atof(s.c_str());return ok;}
inline bool gn(char *s) {string s1;bool ok=gn(s1);strcpy(s,s1.c_str());return ok;}
inline bool gets(char *s) {char c,t=0;while(c=gchar(),c!=-1&&c!='\n')*s++=c,t=1;*s=0;return c!=-1 || t;};

inline void print(int x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(INT x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(char &x){putchar(x);}
inline void print(DO &x){printf("%.10lf", x);}
inline void print(char *s){printf("%s", s);}
inline void print(string &s){printf("%s", s.c_str());}

template<class T,class T1>inline bool gn(T &x, T1 &y) {bool ok=gn(x); gn(y);return ok;}
template<class T,class T1,class T2>inline bool gn(T &x, T1 &y, T2 &z){bool ok=gn(x); gn(y); gn(z);return ok;}
template<class U,class V,class W,class X>inline bool gn(U &u, V &v, W &w, X &x){bool ok=gn(u,v);gn(w,x);return ok;}
template<class T>inline void printsp(T x) {print(x);putchar(' ');}
template<class T>inline void println(T x) {print(x);putchar('\n');}
template<class T,class U>inline void print(T x, U y){printsp(x);println(y);}
template<class T,class U,class V>inline void print(T x, U y, V z){printsp(x);print(y,z);}
template<class T,class U,class V,class W>inline void print(T x, U y, V z,W w){printsp(x);print(y, z, w);}

const int inf=0x3f3f3f3f,mod=1000000007;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

const int NN=1000100;
int hd[NN];
int nx[NN<<1];
int to[NN<<1];
int E;

inline void add(int u,int v){
  nx[E]=hd[u];
  to[E]=v;
  hd[u]=E++;
  nx[E]=hd[v];
  to[E]=u;
  hd[v]=E++;
}

int a[NN],M;
INT arr[NN],m;
INT sum[NN];
unordered_map<INT,int> is_in;

void search(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(fa==v) continue;
    search(v,u);
    sum[u]+=sum[v];
  }
  is_in[sum[u]]++;
}

int dp[NN];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  memset(hd,-1,sizeof(hd));
  int n; gn(n);
  for(int i=1; i<=n; i++)gn(a[i]);
  for(int i=2,d; i<=n; i++){
    gn(d);add(i,d);
  }
  search(1);
  for(int i=1; i<=n; i++) if(sum[1]%sum[i]==0 && sum[1]/sum[i]<=n){
    arr[m++]=sum[i];
  }

  sort(arr,arr+m);
  m=unique(arr,arr+m)-arr;

  for(int i=0; i<m; i++){
    int cnt=0;
    INT tmp=arr[i];
    while(tmp<=sum[1]){
      if(is_in.count(tmp)) cnt+=is_in[tmp];
      tmp+=arr[i];
    }
    if(cnt*arr[i]==sum[1]) arr[M++]=arr[i];
  }
  m=M;

  for(int i=0; i<m; i++){
    dp[i]=1;
    for(int j=0; j<i; j++){
      if(arr[i]%arr[j]==0) {
        dp[i]+=dp[j];
        if(dp[i]>=mod) dp[i]-=mod;
      }
    }
  }

  cout<<dp[m-1]<<endl;

  return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 1000100
VI adj[NN];
INT sz[NN];
int a[NN];

int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	for(auto v : adj[u]) {
		if(v == fa) continue;
		pre_dfs(v, u);
		sz[u] += sz[v];
	}
}
int arr[NN];
int flag[NN];
INT sum[NN];
int calc(int u, int fa, INT val) {
	sum[u] = a[u];
	for(auto v : adj[u]) {
		if(v == fa) continue;
		if(!calc(v, u, val)) return 0;
		sum[u] += sum[v];
	}
	if(sum[u] == val) sum[u] = 0;
	if(sum[u] > val) return 0;
	return 1;
}

VI res[NN];
int dp[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n; gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		adj[x].pb(i);
	}
	pre_dfs(1);
	int nn = 1;
	for(int i=1; i<=n; i++) if(sz[1] % sz[i] == 0) {
		if(calc(1, 0, sz[i])) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
VI adj[NN];
INT sz[NN];
int a[NN];

int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	for(auto v : adj[u]) {
		if(v == fa) continue;
		pre_dfs(v, u);
		sz[u] += sz[v];
	}
}
INT arr[NN];
INT sum[NN];
int calc(int u, int fa, INT val) {
	sum[u] = a[u];
	for(auto v : adj[u]) {
		if(v == fa) continue;
		if(!calc(v, u, val)) return 0;
		sum[u] += sum[v];
	}
	if(sum[u] == val) sum[u] = 0;
	if(sum[u] > val) return 0;
	return 1;
}
VI res[NN];
int dp[NN];
VI rev[NN];
int deg[NN];
int ARR[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n; gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		adj[x].pb(i);
		deg[x]++;
		rev[i].pb(x);
	}
	pre_dfs(1);
	int nn = 1;
	for(int i=1; i<=n; i++) if(sz[1] % sz[i] == 0 and sz[1]/sz[i] <=n) {
		if(calc(1, 0, sz[i])) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	if(nn > 100) assert(0);
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
int to[2*NN], nxt[2*NN], head[NN];
int E = 0;
int son[NN];
void add_edge(int u, int v){
	to[E]=v; nxt[E]=head[u]; head[u]=E++;
	to[E]=u; nxt[E]=head[v]; head[v]=E++;
}
INT sz[NN];
int a[NN];
int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	son[u] = 1;
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		pre_dfs(v, u);
		son[u] += son[v];
		sz[u] += sz[v];
	}
}
INT arr[NN];
INT sum[NN];
int calc(int u, int fa, INT val) {
	sum[u] = a[u];
	for(int e=head[u]; e!=-1; e=nxt[e]){
		int v = to[e];
		if(v == fa) continue;
		if(!calc(v, u, val)) return 0;
		sum[u] += sum[v];
	}
	if(sum[u] == val) sum[u] = 0;
	if(sum[u] > val) return 0;
	return 1;
}
VI res[NN/10];
int dp[NN];
int root;
int n;
int rdfs(int u, int fa = 0) {
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		if(son[v] > n/2) return rdfs(v, u);
	}
	return u;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	memset(head, -1, sizeof head);
	gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		add_edge(x, i);
	}
	root = rdfs(1);
	pre_dfs(root);
	int nn = 1;
	for(int i=1; i<=n; i++) if(sz[root] % sz[i] == 0 and sz[root]/sz[i] <=n) {
		nn++;
	}
	if(nn > 200) assert(0);
	nn = 1;
	for(int i=1; i<=n; i++) if(sz[root] % sz[i] == 0 and sz[root]/sz[i] <=n) {
		if(calc(root, 0, sz[i])) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
int to[2*NN], nxt[2*NN], head[NN];
int E = 0;
void add_edge(int u, int v){
	to[E]=v; nxt[E]=head[u]; head[u]=E++;
	to[E]=u; nxt[E]=head[v]; head[v]=E++;
}
INT sz[NN];
int a[NN];
int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		pre_dfs(v, u);
		sz[u] += sz[v];
	}
}
INT arr[NN];
int dp[NN];
int root;
int n;
map<INT, int> mp;
int calc(INT u) {
	INT val = u;
	int ans = 0;
	while(val <= sz[1]) {
		if(mp.count(val)) ans += mp[val];
		val += u;
	}
	if(u * ans == sz[1]) return 1;
	return 0;
}
VI res[NN/10];
INT ARR[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	memset(head, -1, sizeof head);
	gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		add_edge(x, i);
	}
	pre_dfs(1);
	int nn = 1;
	for(int i=1; i<=n; i++) mp[sz[i]]++;
	for(int i=1; i<=n; i++) if(sz[1] % sz[i] == 0 and sz[1]/sz[i] <=n) {
		arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	int mm = 1;
	for(int i=1; i<nn; i++) if(calc(arr[i])) {
		ARR[mm++] = arr[i];
	}
	for(int i=1; i<mm; i++) arr[i] = ARR[i];
	nn = mm;
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 1000100
VI adj[NN];
INT sz[NN];
int a[NN];

int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	for(auto v : adj[u]) {
		if(v == fa) continue;
		pre_dfs(v, u);
		sz[u] += sz[v];
	}
}
INT arr[NN];
INT sum[NN];
int calc(int u, int fa, INT val) {
	sum[u] = a[u];
	for(auto v : adj[u]) {
		if(v == fa) continue;
		if(!calc(v, u, val)) return 0;
		sum[u] += sum[v];
	}
	if(sum[u] == val) sum[u] = 0;
	if(sum[u] > val) return 0;
	return 1;
}

VI res[NN];
int dp[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n; gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		adj[x].pb(i);
	}
	pre_dfs(1);
	int nn = 1;
	for(int i=1; i<=n; i++) if(sz[1] % sz[i] == 0) {
		if(calc(1, 0, sz[i])) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
VI adj[NN];
INT sz[NN];
int a[NN];

int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	for(auto v : adj[u]) {
		if(v == fa) continue;
		pre_dfs(v, u);
		sz[u] += sz[v];
	}
}
INT arr[NN];
INT sum[NN];
int calc(int u, int fa, INT val) {
	sum[u] = a[u];
	for(auto v : adj[u]) {
		if(v == fa) continue;
		if(!calc(v, u, val)) return 0;
		sum[u] += sum[v];
	}
	if(sum[u] == val) sum[u] = 0;
	if(sum[u] > val) return 0;
	return 1;
}
VI res[NN];
int dp[NN];
VI rev[NN];
int deg[NN];
int ARR[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n; gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		adj[x].pb(i);
		deg[x]++;
		rev[i].pb(x);
	}
	pre_dfs(1);
	int nn = 1;
	for(int i=1; i<=n; i++) if(sz[1] % sz[i] == 0 and sz[1]/sz[i] <=n) {
		if(calc(1, 0, sz[i])) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	if(nn > 200) assert(0);
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
int to[2*NN], nxt[2*NN], head[NN];
int E = 0;
void add_edge(int u, int v){
	to[E]=v; nxt[E]=head[u]; head[u]=E++;
	to[E]=u; nxt[E]=head[v]; head[v]=E++;
}
INT sz[NN];
int a[NN];
int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		pre_dfs(v, u);
		sz[u] += sz[v];
	}
}
INT arr[NN];
int dp[NN];
int root;
int n;
map<INT, int> mp;
int calc(int u) {
	INT val = sz[u];
	int ans = 0;
	while(val <= sz[1]) {
		if(mp.count(val)) ans += mp[val];
		val += sz[u];
	}
	if(sz[u] * ans == sz[1]) return 1;
	return 0;
}
VI res[NN/10];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	memset(head, -1, sizeof head);
	gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		add_edge(x, i);
	}
	pre_dfs(1);
	int nn = 1;
	for(int i=1; i<=n; i++) mp[sz[i]]++;
	for(int i=1; i<=n; i++) if(sz[1] % sz[i] == 0 and sz[1]/sz[i] <=n) {
		if(calc(i)) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sq(x) ((x)*(x))
using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
#define INPUT_LEN 1000
#define gets my_gets
char gchar() {static char tbuf1[INPUT_LEN],*tmp_st=tbuf1,*tmp_ed=tbuf1;if(tmp_st==tmp_ed){int len=fread(tbuf1,1,INPUT_LEN,stdin);if (len==0)return -1;tmp_ed=tbuf1+len;tmp_st=tbuf1;}return*(tmp_st++);}
inline bool gn(int &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(INT &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(char &x) {while(x=gchar(),x!=-1&&isspace(x));return x!=-1;}
inline bool gn(string &s){char c;s.clear();while(c=gchar(),c!=-1&&isspace(c));if(c!=-1)s+=c;while(c=gchar(),c!=-1&&!isspace(c))s+=c;return c!=-1;}
inline bool gn(DO &x) {string s;bool ok=gn(s);x=atof(s.c_str());return ok;}
inline bool gn(char *s) {string s1;bool ok=gn(s1);strcpy(s,s1.c_str());return ok;}
inline bool gets(char *s) {char c,t=0;while(c=gchar(),c!=-1&&c!='\n')*s++=c,t=1;*s=0;return c!=-1 || t;};

inline void print(int x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(INT x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(char &x){putchar(x);}
inline void print(DO &x){printf("%.10lf", x);}
inline void print(char *s){printf("%s", s);}
inline void print(string &s){printf("%s", s.c_str());}

template<class T,class T1>inline bool gn(T &x, T1 &y) {bool ok=gn(x); gn(y);return ok;}
template<class T,class T1,class T2>inline bool gn(T &x, T1 &y, T2 &z){bool ok=gn(x); gn(y); gn(z);return ok;}
template<class U,class V,class W,class X>inline bool gn(U &u, V &v, W &w, X &x){bool ok=gn(u,v);gn(w,x);return ok;}
template<class T>inline void printsp(T x) {print(x);putchar(' ');}
template<class T>inline void println(T x) {print(x);putchar('\n');}
template<class T,class U>inline void print(T x, U y){printsp(x);println(y);}
template<class T,class U,class V>inline void print(T x, U y, V z){printsp(x);print(y,z);}
template<class T,class U,class V,class W>inline void print(T x, U y, V z,W w){printsp(x);print(y, z, w);}

const int inf=0x3f3f3f3f,mod=1000000007;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

inline int power(int a, int b, int m=mod, int ans=1) {
  for(; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
  return ans;
}

const int NN=1000100;
int hd[NN];
int nx[NN<<1];
int to[NN<<1];
int E;

inline void add(int u,int v){
  nx[E]=hd[u];
  to[E]=v;
  hd[u]=E++;
  nx[E]=hd[v];
  to[E]=u;
  hd[v]=E++;
}

int a[NN];
INT arr[100000],m;
INT sum[NN],key;

bool dfs(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(v==fa) continue;
    if(!dfs(v,u)) return 0;
    sum[u]+=sum[v];
  }
  if(sum[u]>key) return 0;
  if(sum[u]==key) sum[u]=0;
  return 1;
}

int dp[100000];
bool can[100000];
bool flag[100000];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  memset(hd,-1,sizeof(hd));
  int n;
  gn(n);
  INT tot=0;
  for(int i=1; i<=n; i++) {
    gn(a[i]);
    tot+=a[i];
  }
  for(int i=2,d; i<=n; i++){
    gn(d);
    add(i,d);
  }

  INT tmp=tot;
  for(int i=1; (INT)i*i<=tmp; i++) if(tmp%i==0){
    arr[m++]=i;
    arr[m++]=tmp/i;
  }

  sort(arr,arr+m);
  m=unique(arr,arr+m)-arr;

  for(int i=m-1; i>=0; i--)if(!flag[i]){
    key=arr[i];
    if(dfs(1)) {
      if(sum[1]==0) can[i]=1;
    }
//    if(!can[i]){
//      for(int j=0; j<i; j++) if(arr[i]%arr[j]==0) flag[j]=1;
//    }
  }

  for(int i=0; i<m; i++){
    if(can[i]){
      dp[i]=1;
      for(int j=0; j<i; j++) if(can[j] && arr[i]%arr[j]==0){
        dp[i]+=dp[j];
        if(dp[i]>=mod) dp[i]-=mod;
      }
    }
  }

  cout<<dp[m-1]<<endl;

  return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sq(x) ((x)*(x))
using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
#define INPUT_LEN 1000
#define gets my_gets
char gchar() {static char tbuf1[INPUT_LEN],*tmp_st=tbuf1,*tmp_ed=tbuf1;if(tmp_st==tmp_ed){int len=fread(tbuf1,1,INPUT_LEN,stdin);if (len==0)return -1;tmp_ed=tbuf1+len;tmp_st=tbuf1;}return*(tmp_st++);}
inline bool gn(int &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(INT &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(char &x) {while(x=gchar(),x!=-1&&isspace(x));return x!=-1;}
inline bool gn(string &s){char c;s.clear();while(c=gchar(),c!=-1&&isspace(c));if(c!=-1)s+=c;while(c=gchar(),c!=-1&&!isspace(c))s+=c;return c!=-1;}
inline bool gn(DO &x) {string s;bool ok=gn(s);x=atof(s.c_str());return ok;}
inline bool gn(char *s) {string s1;bool ok=gn(s1);strcpy(s,s1.c_str());return ok;}
inline bool gets(char *s) {char c,t=0;while(c=gchar(),c!=-1&&c!='\n')*s++=c,t=1;*s=0;return c!=-1 || t;};

inline void print(int x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(INT x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(char &x){putchar(x);}
inline void print(DO &x){printf("%.10lf", x);}
inline void print(char *s){printf("%s", s);}
inline void print(string &s){printf("%s", s.c_str());}

template<class T,class T1>inline bool gn(T &x, T1 &y) {bool ok=gn(x); gn(y);return ok;}
template<class T,class T1,class T2>inline bool gn(T &x, T1 &y, T2 &z){bool ok=gn(x); gn(y); gn(z);return ok;}
template<class U,class V,class W,class X>inline bool gn(U &u, V &v, W &w, X &x){bool ok=gn(u,v);gn(w,x);return ok;}
template<class T>inline void printsp(T x) {print(x);putchar(' ');}
template<class T>inline void println(T x) {print(x);putchar('\n');}
template<class T,class U>inline void print(T x, U y){printsp(x);println(y);}
template<class T,class U,class V>inline void print(T x, U y, V z){printsp(x);print(y,z);}
template<class T,class U,class V,class W>inline void print(T x, U y, V z,W w){printsp(x);print(y, z, w);}

const int inf=0x3f3f3f3f,mod=1000000007;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

inline int power(int a, int b, int m=mod, int ans=1) {
  for(; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
  return ans;
}

const int NN=1000100;
int hd[NN];
int nx[NN<<1];
int to[NN<<1];
int E;

inline void add(int u,int v){
  nx[E]=hd[u];
  to[E]=v;
  hd[u]=E++;
  nx[E]=hd[v];
  to[E]=u;
  hd[v]=E++;
}

int a[NN];
INT arr[100000],m;
INT sum[NN],key;

void search(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(fa==v) continue;
    search(v,u);
    sum[u]+=sum[v];
  }
}

bool dfs(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(v==fa) continue;
    if(!dfs(v,u)) return 0;
    sum[u]+=sum[v];
  }
  if(sum[u]>key) return 0;
  if(sum[u]==key) sum[u]=0;
  return 1;
}

int dp[100000];
bool can[100000];
bool flag[100000];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  memset(hd,-1,sizeof(hd));
  int n;
  gn(n);
  INT tot=0;
  for(int i=1; i<=n; i++) {
    gn(a[i]);
    tot+=a[i];
  }
  for(int i=2,d; i<=n; i++){
    gn(d);
    add(i,d);
  }

  search(1);
  for(int i=1; i<=n; i++) if(sum[1]%sum[i]==0 && sum[1]<=sum[i]*n){
    arr[m++]=sum[i];
  }

  sort(arr,arr+m);
  m=unique(arr,arr+m)-arr;

  for(int i=m-1; i>=0; i--)if(!flag[i]){
    key=arr[i];
    if(dfs(1)) {
      if(sum[1]==0) can[i]=1;
    }
  }

  for(int i=0; i<m; i++){
    if(can[i]){
      dp[i]=1;
      for(int j=0; j<i; j++) if(can[j] && arr[i]%arr[j]==0){
        dp[i]+=dp[j];
        if(dp[i]>=mod) dp[i]-=mod;
      }
    }
  }

  cout<<dp[m-1]<<endl;

  return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sq(x) ((x)*(x))
using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
#define INPUT_LEN 1000
#define gets my_gets
char gchar() {static char tbuf1[INPUT_LEN],*tmp_st=tbuf1,*tmp_ed=tbuf1;if(tmp_st==tmp_ed){int len=fread(tbuf1,1,INPUT_LEN,stdin);if (len==0)return -1;tmp_ed=tbuf1+len;tmp_st=tbuf1;}return*(tmp_st++);}
inline bool gn(int &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(INT &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(char &x) {while(x=gchar(),x!=-1&&isspace(x));return x!=-1;}
inline bool gn(string &s){char c;s.clear();while(c=gchar(),c!=-1&&isspace(c));if(c!=-1)s+=c;while(c=gchar(),c!=-1&&!isspace(c))s+=c;return c!=-1;}
inline bool gn(DO &x) {string s;bool ok=gn(s);x=atof(s.c_str());return ok;}
inline bool gn(char *s) {string s1;bool ok=gn(s1);strcpy(s,s1.c_str());return ok;}
inline bool gets(char *s) {char c,t=0;while(c=gchar(),c!=-1&&c!='\n')*s++=c,t=1;*s=0;return c!=-1 || t;};

inline void print(int x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(INT x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(char &x){putchar(x);}
inline void print(DO &x){printf("%.10lf", x);}
inline void print(char *s){printf("%s", s);}
inline void print(string &s){printf("%s", s.c_str());}

template<class T,class T1>inline bool gn(T &x, T1 &y) {bool ok=gn(x); gn(y);return ok;}
template<class T,class T1,class T2>inline bool gn(T &x, T1 &y, T2 &z){bool ok=gn(x); gn(y); gn(z);return ok;}
template<class U,class V,class W,class X>inline bool gn(U &u, V &v, W &w, X &x){bool ok=gn(u,v);gn(w,x);return ok;}
template<class T>inline void printsp(T x) {print(x);putchar(' ');}
template<class T>inline void println(T x) {print(x);putchar('\n');}
template<class T,class U>inline void print(T x, U y){printsp(x);println(y);}
template<class T,class U,class V>inline void print(T x, U y, V z){printsp(x);print(y,z);}
template<class T,class U,class V,class W>inline void print(T x, U y, V z,W w){printsp(x);print(y, z, w);}

const int inf=0x3f3f3f3f,mod=1000000007;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

inline int power(int a, int b, int m=mod, int ans=1) {
  for(; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
  return ans;
}

const int NN=1000100;
int hd[NN];
int nx[NN<<1];
int to[NN<<1];
int E;

inline void add(int u,int v){
  nx[E]=hd[u];
  to[E]=v;
  hd[u]=E++;
  nx[E]=hd[v];
  to[E]=u;
  hd[v]=E++;
}

int a[NN];
INT arr[100000],m;
INT sum[NN],key;

void search(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(fa==v) continue;
    search(v,u);
    sum[u]+=sum[v];
  }
}

bool dfs(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(v==fa) continue;
    if(!dfs(v,u)) return 0;
    sum[u]+=sum[v];
  }
  if(sum[u]>key) return 0;
  if(sum[u]==key) sum[u]=0;
  return 1;
}

int dp[100000];
bool can[100000];
bool flag[100000];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  memset(hd,-1,sizeof(hd));
  int n;
  gn(n);
  INT tot=0;
  for(int i=1; i<=n; i++) {
    gn(a[i]);
    tot+=a[i];
  }
  for(int i=2,d; i<=n; i++){
    gn(d);
    add(i,d);
  }

  search(1);
  for(int i=1; i<=n; i++) if(sum[1]%sum[i]==0){
    arr[m++]=sum[i];
  }
  sort(arr,arr+m);
  m=unique(arr,arr+m)-arr;

  for(int i=m-1; i>=0; i--)if(!flag[i]){
    key=arr[i];
    if(dfs(1)) {
      if(sum[1]==0) can[i]=1;
    }
  }

  for(int i=0; i<m; i++){
    if(can[i]){
      dp[i]=1;
      for(int j=0; j<i; j++) if(can[j] && arr[i]%arr[j]==0){
        dp[i]+=dp[j];
        if(dp[i]>=mod) dp[i]-=mod;
      }
    }
  }

  cout<<dp[m-1]<<endl;

  return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sq(x) ((x)*(x))
using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
#define INPUT_LEN 1000
#define gets my_gets
char gchar() {static char tbuf1[INPUT_LEN],*tmp_st=tbuf1,*tmp_ed=tbuf1;if(tmp_st==tmp_ed){int len=fread(tbuf1,1,INPUT_LEN,stdin);if (len==0)return -1;tmp_ed=tbuf1+len;tmp_st=tbuf1;}return*(tmp_st++);}
inline bool gn(int &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(INT &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(char &x) {while(x=gchar(),x!=-1&&isspace(x));return x!=-1;}
inline bool gn(string &s){char c;s.clear();while(c=gchar(),c!=-1&&isspace(c));if(c!=-1)s+=c;while(c=gchar(),c!=-1&&!isspace(c))s+=c;return c!=-1;}
inline bool gn(DO &x) {string s;bool ok=gn(s);x=atof(s.c_str());return ok;}
inline bool gn(char *s) {string s1;bool ok=gn(s1);strcpy(s,s1.c_str());return ok;}
inline bool gets(char *s) {char c,t=0;while(c=gchar(),c!=-1&&c!='\n')*s++=c,t=1;*s=0;return c!=-1 || t;};

inline void print(int x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(INT x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(char &x){putchar(x);}
inline void print(DO &x){printf("%.10lf", x);}
inline void print(char *s){printf("%s", s);}
inline void print(string &s){printf("%s", s.c_str());}

template<class T,class T1>inline bool gn(T &x, T1 &y) {bool ok=gn(x); gn(y);return ok;}
template<class T,class T1,class T2>inline bool gn(T &x, T1 &y, T2 &z){bool ok=gn(x); gn(y); gn(z);return ok;}
template<class U,class V,class W,class X>inline bool gn(U &u, V &v, W &w, X &x){bool ok=gn(u,v);gn(w,x);return ok;}
template<class T>inline void printsp(T x) {print(x);putchar(' ');}
template<class T>inline void println(T x) {print(x);putchar('\n');}
template<class T,class U>inline void print(T x, U y){printsp(x);println(y);}
template<class T,class U,class V>inline void print(T x, U y, V z){printsp(x);print(y,z);}
template<class T,class U,class V,class W>inline void print(T x, U y, V z,W w){printsp(x);print(y, z, w);}

const int inf=0x3f3f3f3f,mod=1000000007;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

inline int power(int a, int b, int m=mod, int ans=1) {
  for(; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
  return ans;
}

const int NN=1000100;
int hd[NN];
int nx[NN<<1];
int to[NN<<1];
int E;

inline void add(int u,int v){
  nx[E]=hd[u];
  to[E]=v;
  hd[u]=E++;
  nx[E]=hd[v];
  to[E]=u;
  hd[v]=E++;
}

int a[NN];
INT arr[100000],m;
INT sum[NN],key;

bool dfs(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(v==fa) continue;
    if(!dfs(v,u)) return 0;
    sum[u]+=sum[v];
  }
  if(sum[u]>key) return 0;
  if(sum[u]==key) sum[u]=0;
  return 1;
}

int dp[100000];
bool can[100000];
bool flag[100000];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  memset(hd,-1,sizeof(hd));
  int n;
  gn(n);
  INT tot=0;
  for(int i=1; i<=n; i++) {
    gn(a[i]);
    tot+=a[i];
  }
  for(int i=2,d; i<=n; i++){
    gn(d);
    add(i,d);
  }

  INT tmp=tot;
  for(int i=1; (INT)i*i<=tmp; i++) if(tmp%i==0){
    arr[m++]=i;
    arr[m++]=tmp/i;
  }

  sort(arr,arr+m);
  m=unique(arr,arr+m)-arr;

  for(int i=m-1; i>=0; i--)if(!flag[i]){
    key=arr[i];
    if(dfs(1)) {
      if(sum[1]==0) can[i]=1;
    }
    if(!can[i]){
      for(int j=0; j<i; j++) if(arr[i]%arr[j]==0) flag[i]=1;
    }
  }

  for(int i=0; i<m; i++){
    if(can[i]){
      dp[i]=1;
      for(int j=0; j<i; j++) if(can[j] && arr[i]%arr[j]==0){
        dp[i]+=dp[j];
        if(dp[i]>=mod) dp[i]-=mod;
      }
    }
  }

  cout<<dp[m-1]<<endl;

  return 0;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
int to[2*NN], nxt[2*NN], head[NN];

void add_edge(int u, int v, int c){
	to[E]=v; cap[E]=c; nxt[E]=head[u]; head[u]=E++;
	to[E]=u; cap[E]=0; nxt[E]=head[v]; head[v]=E++;
}
INT sz[NN];
int a[NN];

int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	for(auto v : adj[u]) {
		if(v == fa) continue;
		pre_dfs(v, u);
		sz[u] += sz[v];
	}
}
INT arr[NN];
INT sum[NN];
int calc(int u, int fa, INT val) {
	sum[u] = a[u];
	for(auto v : adj[u]) {
		if(v == fa) continue;
		if(!calc(v, u, val)) return 0;
		sum[u] += sum[v];
	}
	if(sum[u] == val) sum[u] = 0;
	if(sum[u] > val) return 0;
	return 1;
}
VI res[NN];
int dp[NN];
VI rev[NN];
int deg[NN];
int ARR[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n; gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		adj[x].pb(i);
		deg[x]++;
		rev[i].pb(x);
	}
	pre_dfs(1);
	int nn = 1;
	if(nn > 100) assert(0);
	for(int i=1; i<=n; i++) if(sz[1] % sz[i] == 0 and sz[1]/sz[i] <=n) {
		if(calc(1, 0, sz[i])) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
int to[2*NN], nxt[2*NN], head[NN];
int E = 0;
int son[NN];
void add_edge(int u, int v){
	to[E]=v; nxt[E]=head[u]; head[u]=E++;
	to[E]=u; nxt[E]=head[v]; head[v]=E++;
}
INT sz[NN];
int a[NN];
int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	son[u] = 1;
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		pre_dfs(v, u);
		son[u] += son[v];
		sz[u] += sz[v];
	}
}
INT arr[NN];
INT sum[NN];
int calc(int u, int fa, INT val) {
	sum[u] = a[u];
	for(int e=head[u]; e!=-1; e=nxt[e]){
		int v = to[e];
		if(v == fa) continue;
		if(!calc(v, u, val)) return 0;
		sum[u] += sum[v];
	}
	if(sum[u] == val) sum[u] = 0;
	if(sum[u] > val) return 0;
	return 1;
}
VI res[NN/10];
int dp[NN];
int root;
int n;
int rdfs(int u, int fa = 0) {
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		if(son[v] > n/2) return rdfs(v, u);
	}
	return u;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	memset(head, -1, sizeof head);
	gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		add_edge(x, i);
	}
	root = rdfs(1);
	pre_dfs(root);
	int nn = 1;
	for(int i=1; i<=n; i++) if(sz[root] % sz[i] == 0 and sz[root]/sz[i] <=n) {
		nn++;
	}
	if(nn > 100) assert(0);
	nn = 1;
	for(int i=1; i<=n; i++) if(sz[root] % sz[i] == 0 and sz[root]/sz[i] <=n) {
		if(calc(root, 0, sz[i])) arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f
#define mod 1000000007
#define sq(x) ((x)*(x))
#define bit(x, y) (((x)>>(y))&1)
#define bctz(x) (__builtin_ctz(x))
#define bclz(x) (__builtin_clz(x))
#define bclzl(x) (__builtin_clzll(x))
#define bctzl(x) (__builtin_ctzl(x))
#define bpt(x) (__builtin_popcount(x))
#define bptl(x) (__builtin_popcountll(x))
#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;
typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef vector<int> VI;

template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}

template <class T> inline void gn(T &x) {char c, sg=0; while(c=getchar(), (c>'9' || c<'0') && c!='-'); for((c=='-' ? sg=1, c=getchar() : 0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0'; if(sg) x=-x;}
template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) {gn(x1), gn(x2);}
template <class T1, class T2, class T3> inline void gn(T1 &x1, T2 &x2, T3 &x3) {gn(x1, x2), gn(x3);}
template <class T1, class T2, class T3, class T4> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4) {gn(x1, x2, x3), gn(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void gn(T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {gn(x1, x2, x3, x4), gn(x5);}

template <class T> inline void print(T x) {if(x<0) {putchar('-'); return print(-x);} if(x<10) {putchar('0'+x); return ;} print(x/10); putchar(x%10+'0');}
template <class T> inline void println(T x) {print(x); putchar('\n');}
template <class T> inline void printsp(T x) {print(x); putchar(' ');}
template <class T1, class T2> inline void print(T1 x1, T2 x2) {printsp(x1), println(x2);}
template <class T1, class T2, class T3> inline void print(T1 x1, T2 x2, T3 x3) {printsp(x1), printsp(x2), println(x3);}
template <class T1, class T2, class T3, class T4> inline void print(T1 x1, T2 x2, T3 x3, T4 x4) {printsp(x1), printsp(x2), printsp(x3), println(x4);}
template <class T1, class T2, class T3, class T4, class T5> inline void print(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5) {printsp(x1), printsp(x2), printsp(x3), printsp(x4), println(x5);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}
#define NN 1000100
int to[2*NN], nxt[2*NN], head[NN];
int E = 0;
void add_edge(int u, int v){
	to[E]=v; nxt[E]=head[u]; head[u]=E++;
	to[E]=u; nxt[E]=head[v]; head[v]=E++;
}
INT sz[NN];
int a[NN];
int pre_dfs(int u, int fa = 0) {
	sz[u] = a[u];
	for(int e=head[u]; e!=-1; e=nxt[e]) {
		int v = to[e];
		if(v == fa) continue;
		pre_dfs(v, u);
		sz[u] += sz[v];
	}
}
INT arr[NN];
int dp[NN];
int root;
int n;
unordered_map<INT, int> mp;

int calc(INT u) {
	INT val = u;
	int ans = 0;
	while(val <= sz[1]) {
		if(mp.count(val)) ans += mp[val];
		val += u;
	}
	if(u * ans == sz[1]) return 1;
	return 0;
}
VI res[NN/10];
INT ARR[NN];
int main(){
	memset(head, -1, sizeof head);
	gn(n);
	for(int i=1; i<=n; i++) {
		gn(a[i]);
	}
	for(int i=2; i<=n; i++) {
		int x; gn(x);
		add_edge(x, i);
	}
	pre_dfs(1);
	int nn = 1;
	for(int i=1; i<=n; i++) mp[sz[i]]++;
	for(int i=1; i<=n; i++) if(sz[1] % sz[i] == 0 and sz[1]/sz[i] <=n) {
		arr[nn++] = sz[i];
	}
	sort(arr+1, arr+nn);
	nn = unique(arr+1, arr+nn) - arr;
	reverse(arr+1, arr+nn);
	int mm = 1;
	for(int i=1; i<nn; i++) if(calc(arr[i])) {
		ARR[mm++] = arr[i];
	}
	for(int i=1; i<mm; i++) arr[i] = ARR[i];
	nn = mm;
	for(int i=1; i<nn; i++) for(int j=i+1; j<nn; j++) {
		if(arr[i] % arr[j] == 0) res[i].pb(j);
	}
	for(int i=nn-1; i; i--) {
		dp[i] = 1;
		for(auto u : res[i]) dp[i] = (dp[i] + dp[u]) % mod;
	}
	cout<<dp[1]<<endl;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sq(x) ((x)*(x))
using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
#define INPUT_LEN 1000
#define gets my_gets
char gchar() {static char tbuf1[INPUT_LEN],*tmp_st=tbuf1,*tmp_ed=tbuf1;if(tmp_st==tmp_ed){int len=fread(tbuf1,1,INPUT_LEN,stdin);if (len==0)return -1;tmp_ed=tbuf1+len;tmp_st=tbuf1;}return*(tmp_st++);}
inline bool gn(int &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(INT &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(char &x) {while(x=gchar(),x!=-1&&isspace(x));return x!=-1;}
inline bool gn(string &s){char c;s.clear();while(c=gchar(),c!=-1&&isspace(c));if(c!=-1)s+=c;while(c=gchar(),c!=-1&&!isspace(c))s+=c;return c!=-1;}
inline bool gn(DO &x) {string s;bool ok=gn(s);x=atof(s.c_str());return ok;}
inline bool gn(char *s) {string s1;bool ok=gn(s1);strcpy(s,s1.c_str());return ok;}
inline bool gets(char *s) {char c,t=0;while(c=gchar(),c!=-1&&c!='\n')*s++=c,t=1;*s=0;return c!=-1 || t;};

inline void print(int x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(INT x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(char &x){putchar(x);}
inline void print(DO &x){printf("%.10lf", x);}
inline void print(char *s){printf("%s", s);}
inline void print(string &s){printf("%s", s.c_str());}

template<class T,class T1>inline bool gn(T &x, T1 &y) {bool ok=gn(x); gn(y);return ok;}
template<class T,class T1,class T2>inline bool gn(T &x, T1 &y, T2 &z){bool ok=gn(x); gn(y); gn(z);return ok;}
template<class U,class V,class W,class X>inline bool gn(U &u, V &v, W &w, X &x){bool ok=gn(u,v);gn(w,x);return ok;}
template<class T>inline void printsp(T x) {print(x);putchar(' ');}
template<class T>inline void println(T x) {print(x);putchar('\n');}
template<class T,class U>inline void print(T x, U y){printsp(x);println(y);}
template<class T,class U,class V>inline void print(T x, U y, V z){printsp(x);print(y,z);}
template<class T,class U,class V,class W>inline void print(T x, U y, V z,W w){printsp(x);print(y, z, w);}

const int inf=0x3f3f3f3f,mod=1000000007;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

inline int power(int a, int b, int m=mod, int ans=1) {
  for(; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
  return ans;
}

const int NN=1000100;
int hd[NN];
int nx[NN<<1];
int to[NN<<1];
int E;

inline void add(int u,int v){
  nx[E]=hd[u];
  to[E]=v;
  hd[u]=E++;
  nx[E]=hd[v];
  to[E]=u;
  hd[v]=E++;
}

int a[NN],M;
INT arr[100000],m;
INT sum[NN];
unordered_map<INT,int> is_in;

void search(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(fa==v) continue;
    search(v,u);
    sum[u]+=sum[v];
  }
  is_in[sum[u]]++;
}

int dp[100000];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  memset(hd,-1,sizeof(hd));
  int n; gn(n);
  for(int i=1; i<=n; i++)gn(a[i]);
  for(int i=2,d; i<=n; i++){
    gn(d);add(i,d);
  }
  search(1);
  for(int i=1; i<=n; i++) if(sum[1]%sum[i]==0 && sum[1]<=sum[i]*n){
    arr[m++]=sum[i];
  }

  sort(arr,arr+m);
  m=unique(arr,arr+m)-arr;

  for(int i=0; i<m; i++){
    int cnt=0;
    INT tmp=arr[i];
    while(tmp<=sum[1]){
      if(is_in.count(tmp)) cnt+=is_in[tmp];
      tmp+=arr[i];
    }
    if(cnt*arr[i]==sum[1]) arr[M++]=arr[i];
  }
  m=M;

  for(int i=0; i<m; i++){
    dp[i]=1;
    for(int j=0; j<i; j++){
      if(arr[i]%arr[j]==0) {
        dp[i]+=dp[j];
        if(dp[i]>=mod) dp[i]-=mod;
      }
    }
  }

  cout<<dp[m-1]<<endl;

  return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sq(x) ((x)*(x))
using namespace std;
typedef double DO;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
#define INPUT_LEN 1000
#define gets my_gets
char gchar() {static char tbuf1[INPUT_LEN],*tmp_st=tbuf1,*tmp_ed=tbuf1;if(tmp_st==tmp_ed){int len=fread(tbuf1,1,INPUT_LEN,stdin);if (len==0)return -1;tmp_ed=tbuf1+len;tmp_st=tbuf1;}return*(tmp_st++);}
inline bool gn(int &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(INT &x) {char c, sg=0;while (c=gchar(),(c>'9'||c<'0')&&c!='-'&&c!=-1);if (c==-1) return 0;for((c=='-'?sg=1,c=gchar():0),x=0;c>='0'&&c<='9';c=gchar())x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;return 1;}
inline bool gn(char &x) {while(x=gchar(),x!=-1&&isspace(x));return x!=-1;}
inline bool gn(string &s){char c;s.clear();while(c=gchar(),c!=-1&&isspace(c));if(c!=-1)s+=c;while(c=gchar(),c!=-1&&!isspace(c))s+=c;return c!=-1;}
inline bool gn(DO &x) {string s;bool ok=gn(s);x=atof(s.c_str());return ok;}
inline bool gn(char *s) {string s1;bool ok=gn(s1);strcpy(s,s1.c_str());return ok;}
inline bool gets(char *s) {char c,t=0;while(c=gchar(),c!=-1&&c!='\n')*s++=c,t=1;*s=0;return c!=-1 || t;};

inline void print(int x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(INT x) {if(x<0){putchar('-');return print(-x);}if(x<10){putchar('0'+x);return;}print(x/10);putchar(x%10+'0');}
inline void print(char &x){putchar(x);}
inline void print(DO &x){printf("%.10lf", x);}
inline void print(char *s){printf("%s", s);}
inline void print(string &s){printf("%s", s.c_str());}

template<class T,class T1>inline bool gn(T &x, T1 &y) {bool ok=gn(x); gn(y);return ok;}
template<class T,class T1,class T2>inline bool gn(T &x, T1 &y, T2 &z){bool ok=gn(x); gn(y); gn(z);return ok;}
template<class U,class V,class W,class X>inline bool gn(U &u, V &v, W &w, X &x){bool ok=gn(u,v);gn(w,x);return ok;}
template<class T>inline void printsp(T x) {print(x);putchar(' ');}
template<class T>inline void println(T x) {print(x);putchar('\n');}
template<class T,class U>inline void print(T x, U y){printsp(x);println(y);}
template<class T,class U,class V>inline void print(T x, U y, V z){printsp(x);print(y,z);}
template<class T,class U,class V,class W>inline void print(T x, U y, V z,W w){printsp(x);print(y, z, w);}

const int inf=0x3f3f3f3f,mod=1000000007;
const INT INF=0x3f3f3f3f3f3f3f3fLL;
const DO pi=acosl(-1.),eps=1e-9;

const int NN=1000100;
int hd[NN];
int nx[NN<<1];
int to[NN<<1];
int E;

inline void add(int u,int v){
  nx[E]=hd[u];
  to[E]=v;
  hd[u]=E++;
  nx[E]=hd[v];
  to[E]=u;
  hd[v]=E++;
}

int a[NN],M;
INT arr[100000],m;
INT sum[NN];
unordered_map<INT,int> is_in;

void search(int u,int fa=0){
  sum[u]=a[u];
  for(int e=hd[u]; ~e; e=nx[e]){
    int v=to[e];
    if(fa==v) continue;
    search(v,u);
    sum[u]+=sum[v];
  }
  is_in[sum[u]]++;
}

int dp[100000];

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  memset(hd,-1,sizeof(hd));
  int n; gn(n);
  for(int i=1; i<=n; i++)gn(a[i]);
  for(int i=2,d; i<=n; i++){
    gn(d);add(i,d);
  }
  search(1);
  for(int i=1; i<=n; i++) if(sum[1]%sum[i]==0 && sum[1]/sum[i]<=n){
    arr[m++]=sum[i];
  }

  sort(arr,arr+m);
  m=unique(arr,arr+m)-arr;

  for(int i=0; i<m; i++){
    int cnt=0;
    INT tmp=arr[i];
    while(tmp<=sum[1]){
      if(is_in.count(tmp)) cnt+=is_in[tmp];
      tmp+=arr[i];
    }
    if(cnt*arr[i]==sum[1]) arr[M++]=arr[i];
  }
  m=M;

  for(int i=0; i<m; i++){
    dp[i]=1;
    for(int j=0; j<i; j++){
      if(arr[i]%arr[j]==0) {
        dp[i]+=dp[j];
        if(dp[i]>=mod) dp[i]-=mod;
      }
    }
  }

  cout<<dp[m-1]<<endl;

  return 0;
}