import sys
input = sys.stdin.buffer.readline
mod = 10 ** 9 + 7

def main():
	n, m = map(int, input().split())
	*s, = map(int, input().split())
	*t, = map(int, input().split())
	dp = [[1] * (m + 1)] + [[1] + [0] * m for _ in range(n)]
	for i in range(n):
		for j in range(m):
			dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j])) % mod
	print(dp[n][m])

if __name__ == '__main__':
	main() import sys sys input = sys.stdin.buffer.readline input sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline mod = 10 ** 9 + 7 mod 10 ** 9 + 7 10 ** 9 10 9 7 def main():
	n, m = map(int, input().split())
	*s, = map(int, input().split())
	*t, = map(int, input().split())
	dp = [[1] * (m + 1)] + [[1] + [0] * m for _ in range(n)]
	for i in range(n):
		for j in range(m):
			dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j])) % mod
	print(dp[n][m]) main n, m = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split m *s, = map(int, input().split()) *s s map(int, input().split()) map int input().split() ().split () input split *t, = map(int, input().split()) *t t map(int, input().split()) map int input().split() ().split () input split [1] + [0] * m for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [1] + [0] * m [1] 1 [0] * m [0] 0 m dp = [[1] * (m + 1)] + [[1] + [0] * m for _ in range(n)] dp [[1] * (m + 1)] + [[1] + [0] * m for _ in range(n)] [[1] * (m + 1)] [1] * (m + 1) [1] 1 m + 1 m 1 [[1] + [0] * m for _ in range(n)] for i in range(n):
		for j in range(m):
			dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j])) % mod
	 i range(n) range n for j in range(m):
			dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j])) % mod
	 j range(m) range m dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j])) % mod dp[i + 1][j + 1] [i + 1] dp i + 1 i 1 j + 1 j 1 (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j])) % mod dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j]) dp[i + 1][j] + dp[i][j + 1] dp[i + 1][j] [i + 1] dp i + 1 i 1 j dp[i][j + 1] [i] dp i j + 1 j 1 dp[i][j] * (s[i] != t[j]) dp[i][j] [i] dp i j s[i] != t[j] s[i] s i t[j] t j mod print(dp[n][m]) print dp[n][m] [n] dp n m if __name__ == '__main__':
	main() __name__ == '__main__' __name__ '__main__' main() main mod = 10 ** 9 + 7 10 ** 9 + 7 mod def main():
	n, m = map(int, input().split())
	*s, = map(int, input().split())
	*t, = map(int, input().split())
	dp = [[1] * (m + 1)] + [[1] + [0] * m for _ in range(n)]
	for i in range(n):
		for j in range(m):
			dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j])) % mod
	print(dp[n][m]) def main():
	n, m = map(int, input().split())
	*s, = map(int, input().split())
	*t, = map(int, input().split())
	dp = [[1] * (m + 1)] + [[1] + [0] * m for _ in range(n)]
	for i in range(n):
		for j in range(m):
			dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j])) % mod
	print(dp[n][m]) main input = sys.stdin.buffer.readline sys.stdin.buffer.readline input
import sys
input = sys.stdin.buffer.readline
mod = 10 ** 9 + 7

def main():
	n, m = map(int, input().split())
	*s, = map(int, input().split())
	*t, = map(int, input().split())
	dp = [[1] * (m + 1)] + [[1] + [0] * m for _ in range(n)]
	for i in range(n):
		for j in range(m):
			dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] * (s[i] != t[j])) % mod
	print(dp[n][m])

if __name__ == '__main__':
	main()