Stretching Streamers

Count ways to draw non-crossing chords among n points on a circle so the graph is a tree, edges only between numbers sharing a factor.

Hard8Dynamic programmingCombinatoricsNumber theoryGeometryNo attempts yetTime limit2sMemory limit512 MB

Problem

Ms. Hall wants to teach her class about common factors. She arranges her students in a circle and assigns each student an integer from 22 to 10910^9 inclusive. She also gives the students crepe paper streamers. The students stretch these streamers between pairs of students and pull them tight, following these rules.

  • Two students can stretch a streamer between them if and only if their assigned integers share a factor other than 11.
  • There is exactly one path, going from streamer to streamer, between any two students in the circle.
  • No two streamers may cross.
  • A student may hold an end of any number of streamers.

Suppose Ms. Hall has four students, and in order around the circle she gives them 30,3,2,4530, 3, 2, 45. In this arrangement there is exactly one way to stretch the streamers:

If the same numbers are given in the order 3,30,2,453, 30, 2, 45, there are three ways to stretch the streamers:

In how many ways can the students hold the streamers while following all of Ms. Hall's rules? Two ways are different if and only if there are two students with a streamer between them in one way and no streamer between them in the other.

Input

The input consists of a single test case.

The first line contains an integer nn (2n3002 \le n \le 300), the number of students.

Each of the next nn lines contains an integer xx (2x1092 \le x \le 10^9). These are the numbers held by the students, in order around the circle. The students stand in a circle, so the last student is adjacent to the first student.

Output

Print the number of ways the students can satisfy the rules, modulo 109+710^9 + 7.