#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=5e5+10,p=1e9+7;
vector<int> e[N];
int n,m,fl=1;
ll ans;
void A(){
    for(int l=1;l<=n;l++)
        ans+=1ll*(n+l)*(n-l+1)/2%p,ans%=p;
    printf("%lld\n",ans);
}
int main(){
    freopen("barrack.in","r",stdin);
    freopen("barrack.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1,u,v;i<=m;i++){
        scanf("%d%d",&u,&v);
        e[u].push_back(v);
        e[v].push_back(u);
        if(v!=u+1&&u!=v+1) fl=0;
    }
    if(fl&&m==n-1) A();
    fclose(stdin);
    fclose(stdout);
	return 0;
}
