#include <bits/stdc++.h>
using i64 = long long;
constexpr int p = 1000000007;
constexpr int maxd = 30;
constexpr int maxm = 4;
constexpr int maxn = maxd * maxm * 2;
template <class x_tp, class y_tp>
inline void inc(x_tp &x, const y_tp y) {
  x += y;
  (p <= x) && (x -= p);
}
template <class _Tp>
inline _Tp Min(const _Tp x, const _Tp y) {
  return x < y ? x : y;
}
template <class _Tp>
inline _Tp Max(const _Tp x, const _Tp y) {
  return x > y ? x : y;
}
std::map<int, int> idxmp;
inline int idx(const int u) {
  if (idxmp[u]) return idxmp[u];
  return idxmp[u] = (int)idxmp.size();
}
struct Edge {
  int v;
  Edge *las;
  inline Edge *init(const int to, Edge *const ls) {
    return v = to, las = ls, this;
  }
} * las[maxn];
inline void lnk(const int u, const int v) {
  static Edge pool[maxn << 1], *alc = pool - 1;
  las[u] = (++alc)->init(v, las[u]);
  las[v] = (++alc)->init(u, las[v]);
}
inline int io() {
  static int _;
  return scanf("%d", &_), _;
}
inline int lg2(int n) {
  for (int lg = 1;; ++lg)
    if (n < (1 << lg)) return lg;
}
const int n = io(), dep = lg2(n);
inline int calcsz(int u) {
  const int b = lg2(u);
  const int total = (1 << (dep - b + 1)) - 1;
  for (int i = b; i != dep; ++i) u = (u << 1 | 1);
  return total - Max(Min(u - n, 1 << (dep - b)), 0);
}
std::pair<int, int> e[maxm];
int sz[maxn];
void precalc(const int u, const int fa) {
  for (Edge *o = las[u]; o; o = o->las)
    if (o->v != fa) {
      sz[u] -= sz[o->v];
      precalc(o->v, u);
    }
}
std::bitset<maxn + 1> vis;
i64 calc(const int u) {
  i64 sum = sz[u];
  vis[u] = true;
  for (Edge *o = las[u]; o; o = o->las)
    if (!vis[o->v]) sum += calc(o->v);
  vis[u] = false;
  return sum;
}
int main() {
  const int m = io();
  idx(1);
  for (int i = 0; i != m; ++i) {
    e[i].first = io();
    e[i].second = io();
    for (int j = e[i].first; j; j >>= 1) idx(j);
    for (int j = e[i].second; j; j >>= 1) idx(j);
  }
  for (const auto it : idxmp) {
    if (it.first != 1) lnk(idx(it.first), idx(it.first >> 1));
    sz[it.second] = calcsz(it.first);
  }
  precalc(1, 0);
  for (int i = 0; i != m; ++i) lnk(idx(e[i].first), idx(e[i].second));
  i64 ans = 0;
  for (int u = 1, total = idxmp.size(); u <= total; ++u)
    ans += calc(u) % p * sz[u] % p;
  printf("%d", (int)(ans % p));
  return 0;
}
