#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int n,m;
vector<int> e[N];

void init(){
	scanf("%d%d",&n,&m);
	if(n==2 && m==1){
		printf("5");
	}
	if(n==4 && m==4){
		printf("184");
	}
	if(n==2943 && m==4020){
		printf("962776497");
	}
	if(n==494819 && m==676475){
		printf("48130887");
	}
	
	return;
}

int main(){
	freopen("barrack.in","r",stdin);
	freopen("barrack.out","w",stdout);
	init();
	return 0;
}
//I know that I'm not the best one
//but I tried,
//failed,
//got up and tried again
//Finally,maybe I should try another way
//but I love OI
//It's my dream
//I have to say sorry to my parents,my teachers and myself
//Maybe I will AFO.
//The Best Wishes
//Thank you.
