M = 1000000007

@memo = []
def functional(n)
  return 1 if n <= 1
  return @memo[n] if @memo[n]
  @memo[n] = (n * functional(n - 1)) % M
end

until (n = gets.to_i).zero?
  word = (0 ... n).to_a
  gets.to_i.times do
    s, t = gets.split.map{|i| i.to_i - 1}
    word[s], word[t] = word[t], word[s]
  end
  c = (0 ... n).to_a
  order = 0
  (0 ... n - 1).each do |i|
    order += functional(n - i - 1) * c.index(word[i])
    c.delete(word[i])
  end
  p order
end