using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC130E2
{
    class Program
    {
        static void Main(string[] args)
        {
            long mod = (long)Math.Pow(10, 9) + 7;
            string[] input = Console.ReadLine().Split();
            long N = int.Parse(input[0]);
            long M = int.Parse(input[1]);
            string[] S = Console.ReadLine().Split().ToArray();
            string[] T = Console.ReadLine().Split().ToArray();
            //文字列Sについて
            //List<string> b1_List = new List<string>();
            Dictionary<string, long> b1_Dict = new Dictionary<string, long>();
            long num_N = (long)Math.Pow(2, N);
            string[] b1 = new string[num_N];
            for (long i = 0; i < num_N; i++)
            {
                string bs = "";
                b1[i] = Convert.ToString(num_N + i, 2);//桁を揃えたいのでnum_Nを加えて、10進数→2進数
                b1[i] = b1[i].Remove(0, 1);//num_Nを削除するために1文字目を削除
                for (int j = 0; j < N; j++)
                {
                    if (b1[i].Substring(j, 1) == "1")
                    {
                        bs = bs + S[j];
                    }
                }
                //b1_List.Add(bs);
                if (b1_Dict.ContainsKey(bs))
                {
                    b1_Dict[bs]++;
                }
                else
                {
                    b1_Dict.Add(bs, 1);
                }
            }
            //文字列Tについて
            //List<string> b2_List = new List<string>();
            Dictionary<string, long> b2_Dict = new Dictionary<string, long>();
            long num_M = (long)Math.Pow(2, M);
            string[] b2 = new string[num_M];
            for (long i = 0; i < num_M; i++)
            {
                string bt = "";
                b2[i] = Convert.ToString(num_M + i, 2);
                b2[i] = b2[i].Remove(0, 1);
                for (int j = 0; j < M; j++)
                {
                    if (b2[i].Substring(j,1)=="1")
                    {
                        bt = bt + T[j];
                    }
                }
                //b2_List.Add(bt);
                if (b2_Dict.ContainsKey(bt))
                {
                    b2_Dict[bt]++;
                }
                else
                {
                    b2_Dict.Add(bt, 1);
                }
            }
            long ans = 0;

            foreach (var item in b1_Dict)
            {
                //List<string> result = b2_List.FindAll(s => s == item);
                //ans += result.Count;
                if (b2_Dict.ContainsKey(item.Key))
                {
                    ans += item.Value * b2_Dict[item.Key]%mod;
                }
            }
            Console.WriteLine(ans%mod);
        }
    }
}
