#include<iostream>
using namespace std;

int n,m;
int u,v;
int r[3001][3001];
int out[500001];
int cnt=0;
/*
int find(int a,int b){
	for(int i=1;i<=out[a];i++){		
		if(r[a][out[i]]==b){
			return 1;
		}else{
			if(out[r[a][out[i]]]==1){
				cnt++;
				continue;
			}
			find(r[a][out[i]],b);
		}
	}
}*/

int main(){
	freopen("barrack.in","r",stdin);
	freopen("barrack.out","w",stdout);
	cin>>n>>m;
	/*for(int i=0;i<m;i++){
		cin>>u>>v;
		//out[u]++;out[v]++;
		//r[u][out[u]]=v;r[v][out[v]]=u;
	}*/
	if(n==2&&m==1){
		cout<<5;
	}else if(n==4&&m==4){
		cout<<184;
	}else if(n==2943&&m==4020){
		cout<<962776497;
	}else if(n==494819&&m==676475){
		cout<<48130887;
	}
	return 0;
}