#include <bits/stdc++.h>
using namespace std;
long long m = 998244353;
long long pw(long long a, long long b) {
  long long res = 1;
  for (; b > 0; b /= 2) {
    if (b % 2 == 1) res = (res * a) % m;
    a = (a * a) % m;
  }
  return res;
}
int main() {
  long long n, ans = 0, ans0 = 0, t = 0;
  cin >> n;
  long long *f = new long long[n + 1], *p = new long long[n + 1];
  p[0] = 1;
  f[0] = 1;
  for (long long i = 1; i <= n; i++) {
    f[i] = (f[i - 1] * i) % m;
    p[i] = pw(f[i], m - 2);
  }
  for (long long i = 1; i <= n; i++) {
    t = (((f[n] * p[i]) % m) * p[n - i]) % m;
    if (i % 2 == 0) t = -t;
    t = (((t * pw(3, i)) % m) * pw(3, n * (n - i))) % m;
    ans = (ans + t) % m;
  }
  ans = (ans * 2) % m;
  for (long long i = 0; i < n; i++) {
    t = (((f[n] * p[i]) % m) * p[n - i]) % m;
    t = (t * 3) % m;
    if (i % 2 == 0) t = -t;
    t = (t * ((pw(1 - pw(3, i), n) - pw(-pw(3, i), n) + m) % m)) % m;
    ans = (ans + t) % m;
  }
  cout << (ans + m) % m << endl;
  return 0;
}
