// C# code for the approach
using System;
public class GFG {
// mod to avoid integer overflow
const int mod = 1000000007;
// dp table
static int[, ] dp = new int[100001, 2];
// recursive Function to count ways to
// form array of size N such that adjcent
// elements chosen are different
static int recur(int i, int j, int[][] Arr, int N)
{
// If already computed state just
// return dp[i][j]
if (dp[i, j] != -1)
return dp[i, j];
// If array of size N is possible
if (i == N)
return 1;
// Answer initialized with value zero
int ans = 0;
// if choosing element from A[]
if (i == 0 || Arr[i - 1][j] != Arr[i][0])
ans = (ans + recur(i + 1, 0, Arr, N)) % mod;
// if choosing element from B[]
if (i == 0 || Arr[i - 1][j] != Arr[i][1])
ans = (ans + recur(i + 1, 1, Arr, N)) % mod;
// Return the final answer
return dp[i, j] = ans;
}
// Function to count ways to form
// array of size N such that adjcent
// elements chosen are different
static void findWays(int[] A, int[] B, int N)
{
// initializing dp table with - 1
for (int i = 0; i < 100001; i++)
for (int j = 0; j < 2; j++)
dp[i, j] = -1;
// making one array for A[] & B[]
int[][] Arr = new int[N][];
// filling Arr
for (int i = 0; i < N; i++) {
Arr[i] = new int[2];
Arr[i][0] = A[i];
Arr[i][1] = B[i];
}
// calling recursive function
Console.WriteLine(recur(0, 0, Arr, N));
}
// Driver Code
public static void Main()
{
// Input 1
int[] A = { 1, 4, 3 }, B = { 2, 2, 4 };
int N = A.Length;
// Function Call
findWays(A, B, N);
// Input 2
int[] A1 = { 1, 2, 3, 4 }, B1 = { 5, 6, 7, 8 };
int N1 = A1.Length;
// Function Call
findWays(A1, B1, N1);
}
}