#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, mod = 998244353;
long long fac[N], ifac[N];
long long ksm(long long a, long long b) {
  long long ans = 1LL;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
void init() {
  fac[0] = fac[1] = ifac[0] = ifac[1] = 1LL;
  for (int i = 2; i <= N - 5; i++) fac[i] = fac[i - 1] * i % mod;
  ifac[N - 5] = ksm(fac[N - 5], mod - 2);
  for (int i = N - 6; i >= 2; i--) ifac[i] = ifac[i + 1] * (i + 1) % mod;
}
long long C(int n, int m) { return fac[n] * ifac[n - m] % mod * ifac[m] % mod; }
int main() {
  init();
  int n;
  scanf("%d", &n);
  long long ans1 = 0, ans2 = 0;
  for (int i = 1; i <= n; i++) {
    ans1 = (ans1 +
            (C(n, i) * ksm(3, 1LL * n * (n - i) + i) % mod * ksm(-1, i + 1)) +
            mod) %
           mod;
  }
  for (int i = 0; i <= n - 1; i++) {
    long long tmp = -ksm(3, i);
    ans2 =
        (ans2 +
         (C(n, i) * ((ksm(tmp + 1, n) % mod - ksm(tmp, n) % mod + mod) % mod) %
          mod * ksm(-1, i + 1)) +
         mod) %
        mod;
  }
  printf("%lld\n", (ans1 * 2 + ans2 * 3) % mod);
  return 0;
}
