using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class ABC
{
    //          int[] split = Console.ReadLine().Split().Select(int.Parse).ToArray();

    public static void Main()
    {
        int[] split = Console.ReadLine().Split().Select(int.Parse).ToArray();
        int N = split[0];
        int M = split[1];
        int[] S = Console.ReadLine().Split().Select(int.Parse).ToArray();
        int[] T = Console.ReadLine().Split().Select(int.Parse).ToArray();

        Tuple<long,int,int>[][] dp = new Tuple<long, int, int>[N][];

        for (int i = 0; i < N; i++)
        {
            dp[i] = new Tuple<long, int, int>[M];
        }

        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {

                if (S[i] == T[j])
                {
                    if (i >= 1 && j >= 1)
                    {
                        if (S[i] == dp[i - 1][j - 1].Item3)
                        {
                            //mada keisanwo okonawanai
                            //kabutte fueteiku
                            dp[i][j] = new Tuple<long, int, int>(dp[i - 1][j - 1].Item1, S[i], dp[i - 1][j - 1].Item3 + 1);
                        }
                        else
                        {
                            dp[i][j] = new Tuple<long, int, int>(dp[i - 1][j - 1].Item1 * magicnumber(dp[i - 1][j - 1].Item3), S[i], 1);
                        }


                    }
                    else
                    {
                        
                            dp[i][j] = new Tuple<long, int, int>(1, S[i], 1);


                    }
                }
                else
                {
                    if (i >= 1 && j >= 1)
                    {
                        long i1j = dp[i - 1][j].Item1 *magicnumber(dp[i-1][j].Item3);
                        long ij1 = dp[i][j - 1].Item1 * magicnumber(dp[i][j - 1].Item3);
                        if (i1j > ij1) dp[i][j] = dp[i - 1][j];
                        else dp[i][j] = dp[i][j - 1];


                    }
                    else
                    {
                        if (i == 0 & j == 0)
                        {
                            dp[i][j] = new Tuple<long, int, int>(1, 0, 0);


                        }
                        else if (i == 0)
                        {

                            dp[i][j] = dp[i][j - 1];
                        }
                        else
                        {
                            dp[i][j] = dp[i - 1][j];
                        }
                    }
                }

                

            }

        }

            dp[N - 1][M - 1] = new Tuple<long, int, int>(dp[N - 1][M - 1].Item1 * magicnumber(dp[N - 1][M - 1].Item3), 0, 0);

        
        Console.WriteLine(dp[N - 1][M - 1].Item1 % 1000000007);

    }

    static long magicnumber(int n)
    {
        long sum = 0;
        for (int i = 0; i <= n; i++)
        {
            long nCr = combination(n, i);
            sum = sum + nCr;
        }
        return sum;

    }

    static long combination(int n, int r)
    {
        if (n == r) return 1;
        if (r == 0) return 1;
        return (combination(n - 1, r - 1) + combination(n - 1, r));

    }

}