import std.conv, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; } int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); } int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); } Int mod(Int)(Int a, Int m) { if ((a %= m) < 0) a += m; return a; } Int gojo(Int)(Int a, Int b, out Int x, out Int y) { if (b != 0) { Int g = gojo(b, a % b, y, x); y -= (a / b) * x; return g; } x = 1; y = 0; return a; } enum MO = 10L^^9 + 7; void main() { try { for (; ; ) { const N = readInt(); auto A = new int[][](2, N); foreach (h; 0 .. 2) { foreach (i; 0 .. N) { A[h][i] = readInt() - 1; } } auto lens = new long[][](2, N); auto rprs = new int[][](2, N); auto poss = new long[][](2, N); foreach (h; 0 .. 2) { auto vis = new bool[N]; foreach (i; 0 .. N) { if (!vis[i]) { for (int j = i; !vis[j]; j = A[h][j]) { vis[j] = true; rprs[h][j] = i; poss[h][j] = lens[h][i]++; } } } } debug { writeln("lens = ", lens); writeln("rprs = ", rprs); writeln("poss = ", poss); } auto entries = new Tuple!(int, int, long, int)[N]; foreach (i; 0 .. N) { const m0 = lens[0][rprs[0][i]]; const m1 = lens[1][rprs[1][i]]; const g = gcd(m0, m1); entries[i] = tuple(rprs[0][i], rprs[1][i], mod(poss[1][i] - poss[0][i], g), i); } entries.sort; long ans; for (int a = 0, b = 0; a < N; a = b) { for (; b < N && entries[a][0 .. 3] == entries[b][0 .. 3]; ++b) {} const m0 = lens[0][entries[a][0]]; const m1 = lens[1][entries[a][1]]; const d = entries[a][2]; long x, y; const g = gojo(m0, m1, x, y); const mm = m0 * (m1 / g); debug { writeln("entries[a .. b] = ", entries[a .. b]); writefln("m0 = %s, m1 = %s, g = %s, x = %s, y = %s", m0, m1, g, x, y); } long[] ts; foreach (c; a .. b) { const i = entries[c][3]; debug { writefln(" %s: (%s, %s)", i, poss[0][i], poss[1][i]); } /* t == poss[0][i] (m0) d + t == poss[1][i] (m1) */ const t0 = poss[0][i]; const t1 = mod(poss[1][i] - d, m1); assert((t1 - t0) % g == 0); const t = mod(t0 + m0 * mod(x * ((t1 - t0) / g), m1 / g), mm); assert(t % m0 == poss[0][i]); assert((d + t) % m1 == poss[1][i]); ts ~= t; } ts.sort; debug { writeln(" ts = ", ts); } foreach (j; 0 .. ts.length) { long k = mod(ts[(j + 1) % $] - ts[j], mm); if (k == 0) { k = mm; } long k0 = k, k1 = k - 1; ((k0 % 2 == 0) ? k0 : k1) /= 2; (ans += (k0 % MO) * (k1 % MO)) %= MO; } } writeln(ans); } } catch (EOFException e) { } }