public class Lab2b
{
	public static void main(String[] args)
	{
		if (args.length > 0)
		{
			new Lab2b(args[0]);
		}
		else
		{
			System.out.println("Must provide an integer level as the first argument");
		}
	}

	public Lab2b(String s)
	{
		int level = 0;

		try
		{
			level = Integer.parseInt(s);
		}
		catch(Exception ex)
		{
			System.out.println("Error parsing " + s + " as an integer");
			System.exit(-1);
		}

		System.out.println();
		System.out.print(level + "!");
		println(level, level-1);
		System.out.println();
	}

	private void println(int maxLevel, int curLevel)
	{
		if (curLevel > 0)
		{
			String s1 = "\t= ";
			for (int i=maxLevel; i>curLevel; i--)
			{
				s1 += i + " x ";
			}
			s1 += curLevel + "!";
			System.out.println(s1);
			curLevel--;

			println(maxLevel, curLevel);

			String s2 = "\t= ";
			for (int i=maxLevel; i>curLevel+2; i--)
			{
				s2 += i + " x ";
			}
			int cf = factorial(curLevel+2);
			System.out.println(s2 + cf);
		}
	}

	private int factorial(int n)
	{
		int f = 1;
		for (int i=1; i<=n; i++)
		{
			f *= i;
		}
		return f;
	}
}

