// Задача "Циклический класс"
// 10-ая Интернет-олимпиада сезона 2007-2008, базовый уровень
// Автор задачи: Федор Царев, tsarev@rain.ifmo.ru
// Автор решения: Федор Царев, tsarev@rain.ifmo.ru

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

public class cyclic_ft implements Runnable {

	private Scanner in;
	private PrintWriter out;
	String fname = "cyclic";

	private void myAssert(boolean e, String message) {
		if (!e) {
			throw new Error("Assertion failure!!! " + message);
		}
	}

	public void solve() {
		int n = in.nextInt();
		myAssert(1 <= n && n <= 1000, "n is out of bounds: " + n);
		int[] p = new int[n];
		boolean[] w = new boolean[n];
		for (int i = 0; i < n; i++) {
			p[i] = in.nextInt();
			myAssert(1 <= p[i] && p[i] <= n, "p[" + i + "] is out of bounds");
			p[i]--;
			myAssert(!w[p[i]], "Number " + (p[i] + 1) + " occured twice");
			w[p[i]] = true;
		}
		ArrayList<Integer> ans = new ArrayList<Integer>();
		w = new boolean[n];
		for (int i = 0; i < n; i++) {
			if (!w[i]) {
				int cl = 0;
				int j = i;
				while (!w[j]) {
					w[j] = true;
					j = p[j];
					cl++;
				}
				ans.add(cl);
			}
		}
		Collections.sort(ans);
		out.println(ans.size());
		for (int x : ans) {
			out.print(x + " ");
		}
	}

	public void run() {
		try {
			solve();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		} finally {
			out.close();
		}		
	}

	public cyclic_ft() {
		try {
			in = new Scanner(new File(fname + ".in"));
			out = new PrintWriter(new File(fname + ".out"));
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}


	public static void main(String[] args) {
		Locale.setDefault(Locale.US);
		new Thread(new cyclic_ft()).start();
	}

}                       